4 * Support functions for Tahvo ASIC
6 * Copyright (C) 2004, 2005 Nokia Corporation
8 * Written by Juha Yrjölä <juha.yrjola@nokia.com>,
9 * David Weinehall <david.weinehall@nokia.com>, and
10 * Mikko Ylinen <mikko.k.ylinen@nokia.com>
12 * This file is subject to the terms and conditions of the GNU General
13 * Public License. See the file "COPYING" in the main directory of this
14 * archive for more details.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <linux/module.h>
27 #include <linux/init.h>
29 #include <linux/config.h>
30 #include <linux/kernel.h>
31 #include <linux/errno.h>
32 #include <linux/device.h>
33 #include <linux/miscdevice.h>
34 #include <linux/poll.h>
36 #include <linux/interrupt.h>
37 #include <linux/platform_device.h>
39 #include <asm/uaccess.h>
41 #include <asm/arch/mux.h>
42 #include <asm/arch/gpio.h>
43 #include <asm/arch/board.h>
51 static int tahvo_initialized;
52 static int tahvo_irq_pin;
53 static int tahvo_is_betty;
55 static struct tasklet_struct tahvo_tasklet;
56 spinlock_t tahvo_lock = SPIN_LOCK_UNLOCKED;
58 static struct completion device_release;
60 struct tahvo_irq_handler_desc {
61 int (*func)(unsigned long);
66 static struct tahvo_irq_handler_desc tahvo_irq_handlers[MAX_TAHVO_IRQ_HANDLERS];
69 * tahvo_read_reg - Read a value from a register in Tahvo
70 * @reg: the register to read from
72 * This function returns the contents of the specified register
74 int tahvo_read_reg(int reg)
76 BUG_ON(!tahvo_initialized);
77 return cbus_read_reg(cbus_host, TAHVO_ID, reg);
81 * tahvo_write_reg - Write a value to a register in Tahvo
82 * @reg: the register to write to
83 * @reg: the value to write to the register
85 * This function writes a value to the specified register
87 void tahvo_write_reg(int reg, u16 val)
89 BUG_ON(!tahvo_initialized);
90 cbus_write_reg(cbus_host, TAHVO_ID, reg, val);
94 * Disable given TAHVO interrupt
96 void tahvo_disable_irq(int id)
101 spin_lock_irqsave(&tahvo_lock, flags);
102 mask = tahvo_read_reg(TAHVO_REG_IMR);
104 tahvo_write_reg(TAHVO_REG_IMR, mask);
105 spin_unlock_irqrestore(&tahvo_lock, flags);
109 * Enable given TAHVO interrupt
111 void tahvo_enable_irq(int id)
116 spin_lock_irqsave(&tahvo_lock, flags);
117 mask = tahvo_read_reg(TAHVO_REG_IMR);
119 tahvo_write_reg(TAHVO_REG_IMR, mask);
120 spin_unlock_irqrestore(&tahvo_lock, flags);
124 * Acknowledge given TAHVO interrupt
126 void tahvo_ack_irq(int id)
128 tahvo_write_reg(TAHVO_REG_IDR, 1 << id);
131 static int tahvo_7bit_backlight;
133 int tahvo_get_backlight_level(void)
137 if (tahvo_7bit_backlight)
141 return tahvo_read_reg(TAHVO_REG_LEDPWMR) & mask;
144 int tahvo_get_max_backlight_level(void)
146 if (tahvo_7bit_backlight)
152 void tahvo_set_backlight_level(int level)
156 max_level = tahvo_get_max_backlight_level();
157 if (level > max_level)
159 tahvo_write_reg(TAHVO_REG_LEDPWMR, level);
163 * TAHVO interrupt handler. Only schedules the tasklet.
165 static irqreturn_t tahvo_irq_handler(int irq, void *dev_id, struct pt_regs *regs)
167 tasklet_schedule(&tahvo_tasklet);
174 static void tahvo_tasklet_handler(unsigned long data)
176 struct tahvo_irq_handler_desc *hnd;
182 id = tahvo_read_reg(TAHVO_REG_IDR);
183 im = ~tahvo_read_reg(TAHVO_REG_IMR);
189 for (i = 0; id != 0; i++, id >>= 1) {
192 hnd = &tahvo_irq_handlers[i];
193 if (hnd->func == NULL) {
194 /* Spurious tahvo interrupt - just ack it */
195 printk(KERN_INFO "Spurious Tahvo interrupt "
197 tahvo_disable_irq(i);
203 * Don't acknowledge the interrupt here
204 * It must be done explicitly
211 * Register the handler for a given TAHVO interrupt source.
213 int tahvo_request_irq(int id, void *irq_handler, unsigned long arg, char *name)
215 struct tahvo_irq_handler_desc *hnd;
217 if (irq_handler == NULL || id >= MAX_TAHVO_IRQ_HANDLERS ||
219 printk(KERN_ERR PFX "Invalid arguments to %s\n",
223 hnd = &tahvo_irq_handlers[id];
224 if (hnd->func != NULL) {
225 printk(KERN_ERR PFX "IRQ %d already reserved\n", id);
228 printk(KERN_INFO PFX "Registering interrupt %d for device %s\n",
230 hnd->func = irq_handler;
232 strlcpy(hnd->name, name, sizeof(hnd->name));
235 tahvo_enable_irq(id);
241 * Unregister the handler for a given TAHVO interrupt source.
243 void tahvo_free_irq(int id)
245 struct tahvo_irq_handler_desc *hnd;
247 if (id >= MAX_TAHVO_IRQ_HANDLERS) {
248 printk(KERN_ERR PFX "Invalid argument to %s\n",
252 hnd = &tahvo_irq_handlers[id];
253 if (hnd->func == NULL) {
254 printk(KERN_ERR PFX "IRQ %d already freed\n", id);
258 tahvo_disable_irq(id);
263 * tahvo_probe - Probe for Tahvo ASIC
264 * @dev: the Tahvo device
266 * Probe for the Tahvo ASIC and allocate memory
267 * for its device-struct if found
269 static int __devinit tahvo_probe(struct device *dev)
271 const struct omap_em_asic_bb5_config * em_asic_config;
274 /* Prepare tasklet */
275 tasklet_init(&tahvo_tasklet, tahvo_tasklet_handler, 0);
277 em_asic_config = omap_get_config(OMAP_TAG_EM_ASIC_BB5,
278 struct omap_em_asic_bb5_config);
279 if (em_asic_config == NULL) {
280 printk(KERN_ERR PFX "Unable to retrieve config data\n");
284 tahvo_initialized = 1;
286 rev = tahvo_read_reg(TAHVO_REG_ASICR);
288 id = (rev >> 8) & 0xff;
290 if ((rev & 0xff) >= 0x50)
291 tahvo_7bit_backlight = 1;
292 } else if (id == 0x0b) {
294 tahvo_7bit_backlight = 1;
296 printk(KERN_ERR "Tahvo/Betty chip not found");
300 printk(KERN_INFO "%s v%d.%d found\n", tahvo_is_betty ? "Betty" : "Tahvo",
301 (rev >> 4) & 0x0f, rev & 0x0f);
303 tahvo_irq_pin = em_asic_config->tahvo_irq_gpio;
305 if ((ret = omap_request_gpio(tahvo_irq_pin)) < 0) {
306 printk(KERN_ERR PFX "Unable to reserve IRQ GPIO\n");
310 /* Set the pin as input */
311 omap_set_gpio_direction(tahvo_irq_pin, 1);
313 /* Rising edge triggers the IRQ */
314 set_irq_type(OMAP_GPIO_IRQ(tahvo_irq_pin), IRQT_RISING);
316 /* Mask all TAHVO interrupts */
317 tahvo_write_reg(TAHVO_REG_IMR, 0xffff);
319 ret = request_irq(OMAP_GPIO_IRQ(tahvo_irq_pin), tahvo_irq_handler, 0,
322 printk(KERN_ERR PFX "Unable to register IRQ handler\n");
323 omap_free_gpio(tahvo_irq_pin);
326 #ifdef CONFIG_CBUS_TAHVO_USER
327 /* Initialize user-space interface */
328 if (tahvo_user_init() < 0) {
329 printk(KERN_ERR "Unable to initialize driver\n");
330 free_irq(OMAP_GPIO_IRQ(tahvo_irq_pin), 0);
331 omap_free_gpio(tahvo_irq_pin);
338 static int tahvo_remove(struct device *dev)
340 #ifdef CONFIG_CBUS_TAHVO_USER
341 tahvo_user_cleanup();
343 /* Mask all TAHVO interrupts */
344 tahvo_write_reg(TAHVO_REG_IMR, 0xffff);
345 free_irq(OMAP_GPIO_IRQ(tahvo_irq_pin), 0);
346 omap_free_gpio(tahvo_irq_pin);
347 tasklet_kill(&tahvo_tasklet);
352 static void tahvo_device_release(struct device *dev)
354 complete(&device_release);
357 static struct device_driver tahvo_driver = {
359 .bus = &platform_bus_type,
360 .probe = tahvo_probe,
361 .remove = tahvo_remove,
364 static struct platform_device tahvo_device = {
368 .release = tahvo_device_release,
373 * tahvo_init - initialise Tahvo driver
375 * Initialise the Tahvo driver and return 0 if everything worked ok
377 static int __init tahvo_init(void)
381 printk(KERN_INFO "Tahvo/Betty driver initialising\n");
383 init_completion(&device_release);
385 if ((ret = driver_register(&tahvo_driver)) < 0)
388 if ((ret = platform_device_register(&tahvo_device)) < 0) {
389 driver_unregister(&tahvo_driver);
398 static void __exit tahvo_exit(void)
400 platform_device_unregister(&tahvo_device);
401 driver_unregister(&tahvo_driver);
402 wait_for_completion(&device_release);
405 EXPORT_SYMBOL(tahvo_request_irq);
406 EXPORT_SYMBOL(tahvo_free_irq);
407 EXPORT_SYMBOL(tahvo_enable_irq);
408 EXPORT_SYMBOL(tahvo_disable_irq);
409 EXPORT_SYMBOL(tahvo_ack_irq);
410 EXPORT_SYMBOL(tahvo_read_reg);
411 EXPORT_SYMBOL(tahvo_write_reg);
412 EXPORT_SYMBOL(tahvo_get_backlight_level);
413 EXPORT_SYMBOL(tahvo_get_max_backlight_level);
414 EXPORT_SYMBOL(tahvo_set_backlight_level);
416 subsys_initcall(tahvo_init);
417 module_exit(tahvo_exit);
419 MODULE_DESCRIPTION("Tahvo ASIC control");
420 MODULE_LICENSE("GPL");
421 MODULE_AUTHOR("Juha Yrjölä, David Weinehall, and Mikko Ylinen");