Commit | Line | Data |
---|---|---|
05c45ca9 RK |
1 | /* |
2 | * linux/drivers/mfd/ucb1x00-core.c | |
3 | * | |
4 | * Copyright (C) 2001 Russell King, All Rights Reserved. | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License. | |
9 | * | |
10 | * The UCB1x00 core driver provides basic services for handling IO, | |
11 | * the ADC, interrupts, and accessing registers. It is designed | |
12 | * such that everything goes through this layer, thereby providing | |
13 | * a consistent locking methodology, as well as allowing the drivers | |
14 | * to be used on other non-MCP-enabled hardware platforms. | |
15 | * | |
16 | * Note that all locks are private to this file. Nothing else may | |
17 | * touch them. | |
18 | */ | |
05c45ca9 RK |
19 | #include <linux/module.h> |
20 | #include <linux/kernel.h> | |
d43c36dc | 21 | #include <linux/sched.h> |
05c45ca9 RK |
22 | #include <linux/slab.h> |
23 | #include <linux/init.h> | |
24 | #include <linux/errno.h> | |
25 | #include <linux/interrupt.h> | |
26 | #include <linux/device.h> | |
a621aaed | 27 | #include <linux/mutex.h> |
c8602edf | 28 | #include <linux/mfd/ucb1x00.h> |
9ca3dc80 | 29 | #include <linux/gpio.h> |
2c08583c | 30 | #include <linux/semaphore.h> |
05c45ca9 | 31 | |
a09e64fb | 32 | #include <mach/hardware.h> |
05c45ca9 | 33 | |
a621aaed | 34 | static DEFINE_MUTEX(ucb1x00_mutex); |
05c45ca9 RK |
35 | static LIST_HEAD(ucb1x00_drivers); |
36 | static LIST_HEAD(ucb1x00_devices); | |
37 | ||
38 | /** | |
39 | * ucb1x00_io_set_dir - set IO direction | |
40 | * @ucb: UCB1x00 structure describing chip | |
41 | * @in: bitfield of IO pins to be set as inputs | |
42 | * @out: bitfield of IO pins to be set as outputs | |
43 | * | |
44 | * Set the IO direction of the ten general purpose IO pins on | |
45 | * the UCB1x00 chip. The @in bitfield has priority over the | |
46 | * @out bitfield, in that if you specify a pin as both input | |
47 | * and output, it will end up as an input. | |
48 | * | |
49 | * ucb1x00_enable must have been called to enable the comms | |
50 | * before using this function. | |
51 | * | |
52 | * This function takes a spinlock, disabling interrupts. | |
53 | */ | |
54 | void ucb1x00_io_set_dir(struct ucb1x00 *ucb, unsigned int in, unsigned int out) | |
55 | { | |
56 | unsigned long flags; | |
57 | ||
58 | spin_lock_irqsave(&ucb->io_lock, flags); | |
59 | ucb->io_dir |= out; | |
60 | ucb->io_dir &= ~in; | |
61 | ||
62 | ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir); | |
63 | spin_unlock_irqrestore(&ucb->io_lock, flags); | |
64 | } | |
65 | ||
66 | /** | |
67 | * ucb1x00_io_write - set or clear IO outputs | |
68 | * @ucb: UCB1x00 structure describing chip | |
69 | * @set: bitfield of IO pins to set to logic '1' | |
70 | * @clear: bitfield of IO pins to set to logic '0' | |
71 | * | |
72 | * Set the IO output state of the specified IO pins. The value | |
73 | * is retained if the pins are subsequently configured as inputs. | |
74 | * The @clear bitfield has priority over the @set bitfield - | |
75 | * outputs will be cleared. | |
76 | * | |
77 | * ucb1x00_enable must have been called to enable the comms | |
78 | * before using this function. | |
79 | * | |
80 | * This function takes a spinlock, disabling interrupts. | |
81 | */ | |
82 | void ucb1x00_io_write(struct ucb1x00 *ucb, unsigned int set, unsigned int clear) | |
83 | { | |
84 | unsigned long flags; | |
85 | ||
86 | spin_lock_irqsave(&ucb->io_lock, flags); | |
87 | ucb->io_out |= set; | |
88 | ucb->io_out &= ~clear; | |
89 | ||
90 | ucb1x00_reg_write(ucb, UCB_IO_DATA, ucb->io_out); | |
91 | spin_unlock_irqrestore(&ucb->io_lock, flags); | |
92 | } | |
93 | ||
94 | /** | |
95 | * ucb1x00_io_read - read the current state of the IO pins | |
96 | * @ucb: UCB1x00 structure describing chip | |
97 | * | |
98 | * Return a bitfield describing the logic state of the ten | |
99 | * general purpose IO pins. | |
100 | * | |
101 | * ucb1x00_enable must have been called to enable the comms | |
102 | * before using this function. | |
103 | * | |
104 | * This function does not take any semaphores or spinlocks. | |
105 | */ | |
106 | unsigned int ucb1x00_io_read(struct ucb1x00 *ucb) | |
107 | { | |
108 | return ucb1x00_reg_read(ucb, UCB_IO_DATA); | |
109 | } | |
110 | ||
9ca3dc80 TK |
111 | static void ucb1x00_gpio_set(struct gpio_chip *chip, unsigned offset, int value) |
112 | { | |
113 | struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio); | |
114 | unsigned long flags; | |
115 | ||
116 | spin_lock_irqsave(&ucb->io_lock, flags); | |
117 | if (value) | |
118 | ucb->io_out |= 1 << offset; | |
119 | else | |
120 | ucb->io_out &= ~(1 << offset); | |
121 | ||
122 | ucb1x00_reg_write(ucb, UCB_IO_DATA, ucb->io_out); | |
123 | spin_unlock_irqrestore(&ucb->io_lock, flags); | |
124 | } | |
125 | ||
126 | static int ucb1x00_gpio_get(struct gpio_chip *chip, unsigned offset) | |
127 | { | |
128 | struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio); | |
129 | return ucb1x00_reg_read(ucb, UCB_IO_DATA) & (1 << offset); | |
130 | } | |
131 | ||
132 | static int ucb1x00_gpio_direction_input(struct gpio_chip *chip, unsigned offset) | |
133 | { | |
134 | struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio); | |
135 | unsigned long flags; | |
136 | ||
137 | spin_lock_irqsave(&ucb->io_lock, flags); | |
138 | ucb->io_dir &= ~(1 << offset); | |
139 | ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir); | |
140 | spin_unlock_irqrestore(&ucb->io_lock, flags); | |
141 | ||
142 | return 0; | |
143 | } | |
144 | ||
145 | static int ucb1x00_gpio_direction_output(struct gpio_chip *chip, unsigned offset | |
146 | , int value) | |
147 | { | |
148 | struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio); | |
149 | unsigned long flags; | |
c23bb602 | 150 | unsigned old, mask = 1 << offset; |
9ca3dc80 TK |
151 | |
152 | spin_lock_irqsave(&ucb->io_lock, flags); | |
c23bb602 | 153 | old = ucb->io_out; |
9ca3dc80 | 154 | if (value) |
c23bb602 | 155 | ucb->io_out |= mask; |
9ca3dc80 | 156 | else |
c23bb602 RK |
157 | ucb->io_out &= ~mask; |
158 | ||
159 | if (old != ucb->io_out) | |
160 | ucb1x00_reg_write(ucb, UCB_IO_DATA, ucb->io_out); | |
161 | ||
162 | if (!(ucb->io_dir & mask)) { | |
163 | ucb->io_dir |= mask; | |
164 | ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir); | |
165 | } | |
9ca3dc80 TK |
166 | spin_unlock_irqrestore(&ucb->io_lock, flags); |
167 | ||
168 | return 0; | |
169 | } | |
170 | ||
05c45ca9 RK |
171 | /* |
172 | * UCB1300 data sheet says we must: | |
173 | * 1. enable ADC => 5us (including reference startup time) | |
174 | * 2. select input => 51*tsibclk => 4.3us | |
175 | * 3. start conversion => 102*tsibclk => 8.5us | |
176 | * (tsibclk = 1/11981000) | |
177 | * Period between SIB 128-bit frames = 10.7us | |
178 | */ | |
179 | ||
180 | /** | |
181 | * ucb1x00_adc_enable - enable the ADC converter | |
182 | * @ucb: UCB1x00 structure describing chip | |
183 | * | |
184 | * Enable the ucb1x00 and ADC converter on the UCB1x00 for use. | |
185 | * Any code wishing to use the ADC converter must call this | |
186 | * function prior to using it. | |
187 | * | |
188 | * This function takes the ADC semaphore to prevent two or more | |
189 | * concurrent uses, and therefore may sleep. As a result, it | |
190 | * can only be called from process context, not interrupt | |
191 | * context. | |
192 | * | |
193 | * You should release the ADC as soon as possible using | |
194 | * ucb1x00_adc_disable. | |
195 | */ | |
196 | void ucb1x00_adc_enable(struct ucb1x00 *ucb) | |
197 | { | |
198 | down(&ucb->adc_sem); | |
199 | ||
200 | ucb->adc_cr |= UCB_ADC_ENA; | |
201 | ||
202 | ucb1x00_enable(ucb); | |
203 | ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr); | |
204 | } | |
205 | ||
206 | /** | |
207 | * ucb1x00_adc_read - read the specified ADC channel | |
208 | * @ucb: UCB1x00 structure describing chip | |
209 | * @adc_channel: ADC channel mask | |
210 | * @sync: wait for syncronisation pulse. | |
211 | * | |
212 | * Start an ADC conversion and wait for the result. Note that | |
213 | * synchronised ADC conversions (via the ADCSYNC pin) must wait | |
214 | * until the trigger is asserted and the conversion is finished. | |
215 | * | |
216 | * This function currently spins waiting for the conversion to | |
217 | * complete (2 frames max without sync). | |
218 | * | |
219 | * If called for a synchronised ADC conversion, it may sleep | |
220 | * with the ADC semaphore held. | |
221 | */ | |
222 | unsigned int ucb1x00_adc_read(struct ucb1x00 *ucb, int adc_channel, int sync) | |
223 | { | |
224 | unsigned int val; | |
225 | ||
226 | if (sync) | |
227 | adc_channel |= UCB_ADC_SYNC_ENA; | |
228 | ||
229 | ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr | adc_channel); | |
230 | ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr | adc_channel | UCB_ADC_START); | |
231 | ||
232 | for (;;) { | |
233 | val = ucb1x00_reg_read(ucb, UCB_ADC_DATA); | |
234 | if (val & UCB_ADC_DAT_VAL) | |
235 | break; | |
236 | /* yield to other processes */ | |
237 | set_current_state(TASK_INTERRUPTIBLE); | |
238 | schedule_timeout(1); | |
239 | } | |
240 | ||
241 | return UCB_ADC_DAT(val); | |
242 | } | |
243 | ||
244 | /** | |
245 | * ucb1x00_adc_disable - disable the ADC converter | |
246 | * @ucb: UCB1x00 structure describing chip | |
247 | * | |
248 | * Disable the ADC converter and release the ADC semaphore. | |
249 | */ | |
250 | void ucb1x00_adc_disable(struct ucb1x00 *ucb) | |
251 | { | |
252 | ucb->adc_cr &= ~UCB_ADC_ENA; | |
253 | ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr); | |
254 | ucb1x00_disable(ucb); | |
255 | ||
256 | up(&ucb->adc_sem); | |
257 | } | |
258 | ||
259 | /* | |
260 | * UCB1x00 Interrupt handling. | |
261 | * | |
262 | * The UCB1x00 can generate interrupts when the SIBCLK is stopped. | |
263 | * Since we need to read an internal register, we must re-enable | |
264 | * SIBCLK to talk to the chip. We leave the clock running until | |
265 | * we have finished processing all interrupts from the chip. | |
266 | */ | |
7d12e780 | 267 | static irqreturn_t ucb1x00_irq(int irqnr, void *devid) |
05c45ca9 RK |
268 | { |
269 | struct ucb1x00 *ucb = devid; | |
270 | struct ucb1x00_irq *irq; | |
271 | unsigned int isr, i; | |
272 | ||
273 | ucb1x00_enable(ucb); | |
274 | isr = ucb1x00_reg_read(ucb, UCB_IE_STATUS); | |
275 | ucb1x00_reg_write(ucb, UCB_IE_CLEAR, isr); | |
276 | ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0); | |
277 | ||
278 | for (i = 0, irq = ucb->irq_handler; i < 16 && isr; i++, isr >>= 1, irq++) | |
279 | if (isr & 1 && irq->fn) | |
280 | irq->fn(i, irq->devid); | |
281 | ucb1x00_disable(ucb); | |
282 | ||
283 | return IRQ_HANDLED; | |
284 | } | |
285 | ||
286 | /** | |
287 | * ucb1x00_hook_irq - hook a UCB1x00 interrupt | |
288 | * @ucb: UCB1x00 structure describing chip | |
289 | * @idx: interrupt index | |
290 | * @fn: function to call when interrupt is triggered | |
291 | * @devid: device id to pass to interrupt handler | |
292 | * | |
293 | * Hook the specified interrupt. You can only register one handler | |
294 | * for each interrupt source. The interrupt source is not enabled | |
295 | * by this function; use ucb1x00_enable_irq instead. | |
296 | * | |
297 | * Interrupt handlers will be called with other interrupts enabled. | |
298 | * | |
299 | * Returns zero on success, or one of the following errors: | |
300 | * -EINVAL if the interrupt index is invalid | |
301 | * -EBUSY if the interrupt has already been hooked | |
302 | */ | |
303 | int ucb1x00_hook_irq(struct ucb1x00 *ucb, unsigned int idx, void (*fn)(int, void *), void *devid) | |
304 | { | |
305 | struct ucb1x00_irq *irq; | |
306 | int ret = -EINVAL; | |
307 | ||
308 | if (idx < 16) { | |
309 | irq = ucb->irq_handler + idx; | |
310 | ret = -EBUSY; | |
311 | ||
312 | spin_lock_irq(&ucb->lock); | |
313 | if (irq->fn == NULL) { | |
314 | irq->devid = devid; | |
315 | irq->fn = fn; | |
316 | ret = 0; | |
317 | } | |
318 | spin_unlock_irq(&ucb->lock); | |
319 | } | |
320 | return ret; | |
321 | } | |
322 | ||
323 | /** | |
324 | * ucb1x00_enable_irq - enable an UCB1x00 interrupt source | |
325 | * @ucb: UCB1x00 structure describing chip | |
326 | * @idx: interrupt index | |
327 | * @edges: interrupt edges to enable | |
328 | * | |
329 | * Enable the specified interrupt to trigger on %UCB_RISING, | |
330 | * %UCB_FALLING or both edges. The interrupt should have been | |
331 | * hooked by ucb1x00_hook_irq. | |
332 | */ | |
333 | void ucb1x00_enable_irq(struct ucb1x00 *ucb, unsigned int idx, int edges) | |
334 | { | |
335 | unsigned long flags; | |
336 | ||
337 | if (idx < 16) { | |
338 | spin_lock_irqsave(&ucb->lock, flags); | |
339 | ||
340 | ucb1x00_enable(ucb); | |
341 | if (edges & UCB_RISING) { | |
342 | ucb->irq_ris_enbl |= 1 << idx; | |
343 | ucb1x00_reg_write(ucb, UCB_IE_RIS, ucb->irq_ris_enbl); | |
344 | } | |
345 | if (edges & UCB_FALLING) { | |
346 | ucb->irq_fal_enbl |= 1 << idx; | |
347 | ucb1x00_reg_write(ucb, UCB_IE_FAL, ucb->irq_fal_enbl); | |
348 | } | |
349 | ucb1x00_disable(ucb); | |
350 | spin_unlock_irqrestore(&ucb->lock, flags); | |
351 | } | |
352 | } | |
353 | ||
354 | /** | |
355 | * ucb1x00_disable_irq - disable an UCB1x00 interrupt source | |
356 | * @ucb: UCB1x00 structure describing chip | |
357 | * @edges: interrupt edges to disable | |
358 | * | |
359 | * Disable the specified interrupt triggering on the specified | |
360 | * (%UCB_RISING, %UCB_FALLING or both) edges. | |
361 | */ | |
362 | void ucb1x00_disable_irq(struct ucb1x00 *ucb, unsigned int idx, int edges) | |
363 | { | |
364 | unsigned long flags; | |
365 | ||
366 | if (idx < 16) { | |
367 | spin_lock_irqsave(&ucb->lock, flags); | |
368 | ||
369 | ucb1x00_enable(ucb); | |
370 | if (edges & UCB_RISING) { | |
371 | ucb->irq_ris_enbl &= ~(1 << idx); | |
372 | ucb1x00_reg_write(ucb, UCB_IE_RIS, ucb->irq_ris_enbl); | |
373 | } | |
374 | if (edges & UCB_FALLING) { | |
375 | ucb->irq_fal_enbl &= ~(1 << idx); | |
376 | ucb1x00_reg_write(ucb, UCB_IE_FAL, ucb->irq_fal_enbl); | |
377 | } | |
378 | ucb1x00_disable(ucb); | |
379 | spin_unlock_irqrestore(&ucb->lock, flags); | |
380 | } | |
381 | } | |
382 | ||
383 | /** | |
384 | * ucb1x00_free_irq - disable and free the specified UCB1x00 interrupt | |
385 | * @ucb: UCB1x00 structure describing chip | |
386 | * @idx: interrupt index | |
387 | * @devid: device id. | |
388 | * | |
389 | * Disable the interrupt source and remove the handler. devid must | |
390 | * match the devid passed when hooking the interrupt. | |
391 | * | |
392 | * Returns zero on success, or one of the following errors: | |
393 | * -EINVAL if the interrupt index is invalid | |
394 | * -ENOENT if devid does not match | |
395 | */ | |
396 | int ucb1x00_free_irq(struct ucb1x00 *ucb, unsigned int idx, void *devid) | |
397 | { | |
398 | struct ucb1x00_irq *irq; | |
399 | int ret; | |
400 | ||
401 | if (idx >= 16) | |
402 | goto bad; | |
403 | ||
404 | irq = ucb->irq_handler + idx; | |
405 | ret = -ENOENT; | |
406 | ||
407 | spin_lock_irq(&ucb->lock); | |
408 | if (irq->devid == devid) { | |
409 | ucb->irq_ris_enbl &= ~(1 << idx); | |
410 | ucb->irq_fal_enbl &= ~(1 << idx); | |
411 | ||
412 | ucb1x00_enable(ucb); | |
413 | ucb1x00_reg_write(ucb, UCB_IE_RIS, ucb->irq_ris_enbl); | |
414 | ucb1x00_reg_write(ucb, UCB_IE_FAL, ucb->irq_fal_enbl); | |
415 | ucb1x00_disable(ucb); | |
416 | ||
417 | irq->fn = NULL; | |
418 | irq->devid = NULL; | |
419 | ret = 0; | |
420 | } | |
421 | spin_unlock_irq(&ucb->lock); | |
422 | return ret; | |
423 | ||
424 | bad: | |
425 | printk(KERN_ERR "Freeing bad UCB1x00 irq %d\n", idx); | |
426 | return -EINVAL; | |
427 | } | |
428 | ||
429 | static int ucb1x00_add_dev(struct ucb1x00 *ucb, struct ucb1x00_driver *drv) | |
430 | { | |
431 | struct ucb1x00_dev *dev; | |
432 | int ret = -ENOMEM; | |
433 | ||
434 | dev = kmalloc(sizeof(struct ucb1x00_dev), GFP_KERNEL); | |
435 | if (dev) { | |
436 | dev->ucb = ucb; | |
437 | dev->drv = drv; | |
438 | ||
439 | ret = drv->add(dev); | |
440 | ||
441 | if (ret == 0) { | |
442 | list_add(&dev->dev_node, &ucb->devs); | |
443 | list_add(&dev->drv_node, &drv->devs); | |
444 | } else { | |
445 | kfree(dev); | |
446 | } | |
447 | } | |
448 | return ret; | |
449 | } | |
450 | ||
451 | static void ucb1x00_remove_dev(struct ucb1x00_dev *dev) | |
452 | { | |
453 | dev->drv->remove(dev); | |
454 | list_del(&dev->dev_node); | |
455 | list_del(&dev->drv_node); | |
456 | kfree(dev); | |
457 | } | |
458 | ||
459 | /* | |
460 | * Try to probe our interrupt, rather than relying on lots of | |
461 | * hard-coded machine dependencies. For reference, the expected | |
462 | * IRQ mappings are: | |
463 | * | |
464 | * Machine Default IRQ | |
465 | * adsbitsy IRQ_GPCIN4 | |
466 | * cerf IRQ_GPIO_UCB1200_IRQ | |
467 | * flexanet IRQ_GPIO_GUI | |
468 | * freebird IRQ_GPIO_FREEBIRD_UCB1300_IRQ | |
469 | * graphicsclient ADS_EXT_IRQ(8) | |
470 | * graphicsmaster ADS_EXT_IRQ(8) | |
471 | * lart LART_IRQ_UCB1200 | |
472 | * omnimeter IRQ_GPIO23 | |
473 | * pfs168 IRQ_GPIO_UCB1300_IRQ | |
474 | * simpad IRQ_GPIO_UCB1300_IRQ | |
475 | * shannon SHANNON_IRQ_GPIO_IRQ_CODEC | |
476 | * yopy IRQ_GPIO_UCB1200_IRQ | |
477 | */ | |
478 | static int ucb1x00_detect_irq(struct ucb1x00 *ucb) | |
479 | { | |
480 | unsigned long mask; | |
481 | ||
482 | mask = probe_irq_on(); | |
cfc73656 IM |
483 | if (!mask) { |
484 | probe_irq_off(mask); | |
05c45ca9 | 485 | return NO_IRQ; |
cfc73656 | 486 | } |
05c45ca9 RK |
487 | |
488 | /* | |
489 | * Enable the ADC interrupt. | |
490 | */ | |
491 | ucb1x00_reg_write(ucb, UCB_IE_RIS, UCB_IE_ADC); | |
492 | ucb1x00_reg_write(ucb, UCB_IE_FAL, UCB_IE_ADC); | |
493 | ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0xffff); | |
494 | ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0); | |
495 | ||
496 | /* | |
497 | * Cause an ADC interrupt. | |
498 | */ | |
499 | ucb1x00_reg_write(ucb, UCB_ADC_CR, UCB_ADC_ENA); | |
500 | ucb1x00_reg_write(ucb, UCB_ADC_CR, UCB_ADC_ENA | UCB_ADC_START); | |
501 | ||
502 | /* | |
503 | * Wait for the conversion to complete. | |
504 | */ | |
505 | while ((ucb1x00_reg_read(ucb, UCB_ADC_DATA) & UCB_ADC_DAT_VAL) == 0); | |
506 | ucb1x00_reg_write(ucb, UCB_ADC_CR, 0); | |
507 | ||
508 | /* | |
509 | * Disable and clear interrupt. | |
510 | */ | |
511 | ucb1x00_reg_write(ucb, UCB_IE_RIS, 0); | |
512 | ucb1x00_reg_write(ucb, UCB_IE_FAL, 0); | |
513 | ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0xffff); | |
514 | ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0); | |
515 | ||
516 | /* | |
517 | * Read triggered interrupt. | |
518 | */ | |
519 | return probe_irq_off(mask); | |
520 | } | |
521 | ||
0c55445f | 522 | static void ucb1x00_release(struct device *dev) |
585f5457 NP |
523 | { |
524 | struct ucb1x00 *ucb = classdev_to_ucb1x00(dev); | |
525 | kfree(ucb); | |
526 | } | |
527 | ||
528 | static struct class ucb1x00_class = { | |
529 | .name = "ucb1x00", | |
0c55445f | 530 | .dev_release = ucb1x00_release, |
585f5457 NP |
531 | }; |
532 | ||
05c45ca9 RK |
533 | static int ucb1x00_probe(struct mcp *mcp) |
534 | { | |
535 | struct ucb1x00 *ucb; | |
536 | struct ucb1x00_driver *drv; | |
537 | unsigned int id; | |
538 | int ret = -ENODEV; | |
9ca3dc80 | 539 | int temp; |
05c45ca9 RK |
540 | |
541 | mcp_enable(mcp); | |
542 | id = mcp_reg_read(mcp, UCB_ID); | |
543 | ||
65f2e753 RK |
544 | if (id != UCB_ID_1200 && id != UCB_ID_1300 && id != UCB_ID_TC35143) { |
545 | printk(KERN_WARNING "UCB1x00 ID not found: %04x\n", id); | |
05c45ca9 RK |
546 | goto err_disable; |
547 | } | |
548 | ||
dd00cc48 | 549 | ucb = kzalloc(sizeof(struct ucb1x00), GFP_KERNEL); |
05c45ca9 RK |
550 | ret = -ENOMEM; |
551 | if (!ucb) | |
552 | goto err_disable; | |
553 | ||
65f2e753 | 554 | |
0c55445f TJ |
555 | ucb->dev.class = &ucb1x00_class; |
556 | ucb->dev.parent = &mcp->attached_device; | |
65f2e753 | 557 | dev_set_name(&ucb->dev, "ucb1x00"); |
05c45ca9 RK |
558 | |
559 | spin_lock_init(&ucb->lock); | |
560 | spin_lock_init(&ucb->io_lock); | |
561 | sema_init(&ucb->adc_sem, 1); | |
562 | ||
65f2e753 | 563 | ucb->id = id; |
05c45ca9 RK |
564 | ucb->mcp = mcp; |
565 | ucb->irq = ucb1x00_detect_irq(ucb); | |
566 | if (ucb->irq == NO_IRQ) { | |
65f2e753 | 567 | printk(KERN_ERR "UCB1x00: IRQ probe failed\n"); |
05c45ca9 RK |
568 | ret = -ENODEV; |
569 | goto err_free; | |
570 | } | |
571 | ||
9ca3dc80 | 572 | ucb->gpio.base = -1; |
65f2e753 | 573 | if (mcp->gpio_base != 0) { |
9ca3dc80 | 574 | ucb->gpio.label = dev_name(&ucb->dev); |
65f2e753 | 575 | ucb->gpio.base = mcp->gpio_base; |
9ca3dc80 TK |
576 | ucb->gpio.ngpio = 10; |
577 | ucb->gpio.set = ucb1x00_gpio_set; | |
578 | ucb->gpio.get = ucb1x00_gpio_get; | |
579 | ucb->gpio.direction_input = ucb1x00_gpio_direction_input; | |
580 | ucb->gpio.direction_output = ucb1x00_gpio_direction_output; | |
581 | ret = gpiochip_add(&ucb->gpio); | |
582 | if (ret) | |
583 | goto err_free; | |
584 | } else | |
585 | dev_info(&ucb->dev, "gpio_base not set so no gpiolib support"); | |
586 | ||
dace1453 | 587 | ret = request_irq(ucb->irq, ucb1x00_irq, IRQF_TRIGGER_RISING, |
65f2e753 | 588 | "UCB1x00", ucb); |
05c45ca9 | 589 | if (ret) { |
65f2e753 RK |
590 | printk(KERN_ERR "ucb1x00: unable to grab irq%d: %d\n", |
591 | ucb->irq, ret); | |
9ca3dc80 | 592 | goto err_gpio; |
05c45ca9 RK |
593 | } |
594 | ||
05c45ca9 RK |
595 | mcp_set_drvdata(mcp, ucb); |
596 | ||
0c55445f | 597 | ret = device_register(&ucb->dev); |
05c45ca9 RK |
598 | if (ret) |
599 | goto err_irq; | |
600 | ||
9ca3dc80 | 601 | |
05c45ca9 | 602 | INIT_LIST_HEAD(&ucb->devs); |
a621aaed | 603 | mutex_lock(&ucb1x00_mutex); |
05c45ca9 RK |
604 | list_add(&ucb->node, &ucb1x00_devices); |
605 | list_for_each_entry(drv, &ucb1x00_drivers, node) { | |
606 | ucb1x00_add_dev(ucb, drv); | |
607 | } | |
a621aaed | 608 | mutex_unlock(&ucb1x00_mutex); |
9ca3dc80 | 609 | |
05c45ca9 RK |
610 | goto out; |
611 | ||
612 | err_irq: | |
613 | free_irq(ucb->irq, ucb); | |
9ca3dc80 TK |
614 | err_gpio: |
615 | if (ucb->gpio.base != -1) | |
616 | temp = gpiochip_remove(&ucb->gpio); | |
05c45ca9 RK |
617 | err_free: |
618 | kfree(ucb); | |
619 | err_disable: | |
620 | mcp_disable(mcp); | |
621 | out: | |
622 | return ret; | |
623 | } | |
624 | ||
625 | static void ucb1x00_remove(struct mcp *mcp) | |
626 | { | |
627 | struct ucb1x00 *ucb = mcp_get_drvdata(mcp); | |
628 | struct list_head *l, *n; | |
9ca3dc80 | 629 | int ret; |
05c45ca9 | 630 | |
a621aaed | 631 | mutex_lock(&ucb1x00_mutex); |
05c45ca9 RK |
632 | list_del(&ucb->node); |
633 | list_for_each_safe(l, n, &ucb->devs) { | |
634 | struct ucb1x00_dev *dev = list_entry(l, struct ucb1x00_dev, dev_node); | |
635 | ucb1x00_remove_dev(dev); | |
636 | } | |
a621aaed | 637 | mutex_unlock(&ucb1x00_mutex); |
05c45ca9 | 638 | |
9ca3dc80 TK |
639 | if (ucb->gpio.base != -1) { |
640 | ret = gpiochip_remove(&ucb->gpio); | |
641 | if (ret) | |
642 | dev_err(&ucb->dev, "Can't remove gpio chip: %d\n", ret); | |
643 | } | |
644 | ||
05c45ca9 | 645 | free_irq(ucb->irq, ucb); |
0c55445f | 646 | device_unregister(&ucb->dev); |
05c45ca9 RK |
647 | } |
648 | ||
05c45ca9 RK |
649 | int ucb1x00_register_driver(struct ucb1x00_driver *drv) |
650 | { | |
651 | struct ucb1x00 *ucb; | |
652 | ||
653 | INIT_LIST_HEAD(&drv->devs); | |
a621aaed | 654 | mutex_lock(&ucb1x00_mutex); |
05c45ca9 RK |
655 | list_add(&drv->node, &ucb1x00_drivers); |
656 | list_for_each_entry(ucb, &ucb1x00_devices, node) { | |
657 | ucb1x00_add_dev(ucb, drv); | |
658 | } | |
a621aaed | 659 | mutex_unlock(&ucb1x00_mutex); |
05c45ca9 RK |
660 | return 0; |
661 | } | |
662 | ||
663 | void ucb1x00_unregister_driver(struct ucb1x00_driver *drv) | |
664 | { | |
665 | struct list_head *n, *l; | |
666 | ||
a621aaed | 667 | mutex_lock(&ucb1x00_mutex); |
05c45ca9 RK |
668 | list_del(&drv->node); |
669 | list_for_each_safe(l, n, &drv->devs) { | |
670 | struct ucb1x00_dev *dev = list_entry(l, struct ucb1x00_dev, drv_node); | |
671 | ucb1x00_remove_dev(dev); | |
672 | } | |
a621aaed | 673 | mutex_unlock(&ucb1x00_mutex); |
05c45ca9 RK |
674 | } |
675 | ||
676 | static int ucb1x00_suspend(struct mcp *mcp, pm_message_t state) | |
677 | { | |
678 | struct ucb1x00 *ucb = mcp_get_drvdata(mcp); | |
679 | struct ucb1x00_dev *dev; | |
680 | ||
a621aaed | 681 | mutex_lock(&ucb1x00_mutex); |
05c45ca9 RK |
682 | list_for_each_entry(dev, &ucb->devs, dev_node) { |
683 | if (dev->drv->suspend) | |
684 | dev->drv->suspend(dev, state); | |
685 | } | |
a621aaed | 686 | mutex_unlock(&ucb1x00_mutex); |
05c45ca9 RK |
687 | return 0; |
688 | } | |
689 | ||
690 | static int ucb1x00_resume(struct mcp *mcp) | |
691 | { | |
692 | struct ucb1x00 *ucb = mcp_get_drvdata(mcp); | |
693 | struct ucb1x00_dev *dev; | |
694 | ||
2e95e51e | 695 | ucb1x00_reg_write(ucb, UCB_IO_DATA, ucb->io_out); |
9ca3dc80 | 696 | ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir); |
a621aaed | 697 | mutex_lock(&ucb1x00_mutex); |
05c45ca9 RK |
698 | list_for_each_entry(dev, &ucb->devs, dev_node) { |
699 | if (dev->drv->resume) | |
700 | dev->drv->resume(dev); | |
701 | } | |
a621aaed | 702 | mutex_unlock(&ucb1x00_mutex); |
05c45ca9 RK |
703 | return 0; |
704 | } | |
705 | ||
706 | static struct mcp_driver ucb1x00_driver = { | |
707 | .drv = { | |
708 | .name = "ucb1x00", | |
709 | }, | |
710 | .probe = ucb1x00_probe, | |
711 | .remove = ucb1x00_remove, | |
712 | .suspend = ucb1x00_suspend, | |
713 | .resume = ucb1x00_resume, | |
714 | }; | |
715 | ||
716 | static int __init ucb1x00_init(void) | |
717 | { | |
718 | int ret = class_register(&ucb1x00_class); | |
719 | if (ret == 0) { | |
720 | ret = mcp_driver_register(&ucb1x00_driver); | |
721 | if (ret) | |
722 | class_unregister(&ucb1x00_class); | |
723 | } | |
724 | return ret; | |
725 | } | |
726 | ||
727 | static void __exit ucb1x00_exit(void) | |
728 | { | |
729 | mcp_driver_unregister(&ucb1x00_driver); | |
730 | class_unregister(&ucb1x00_class); | |
731 | } | |
732 | ||
733 | module_init(ucb1x00_init); | |
734 | module_exit(ucb1x00_exit); | |
735 | ||
05c45ca9 RK |
736 | EXPORT_SYMBOL(ucb1x00_io_set_dir); |
737 | EXPORT_SYMBOL(ucb1x00_io_write); | |
738 | EXPORT_SYMBOL(ucb1x00_io_read); | |
739 | ||
740 | EXPORT_SYMBOL(ucb1x00_adc_enable); | |
741 | EXPORT_SYMBOL(ucb1x00_adc_read); | |
742 | EXPORT_SYMBOL(ucb1x00_adc_disable); | |
743 | ||
744 | EXPORT_SYMBOL(ucb1x00_hook_irq); | |
745 | EXPORT_SYMBOL(ucb1x00_free_irq); | |
746 | EXPORT_SYMBOL(ucb1x00_enable_irq); | |
747 | EXPORT_SYMBOL(ucb1x00_disable_irq); | |
748 | ||
749 | EXPORT_SYMBOL(ucb1x00_register_driver); | |
750 | EXPORT_SYMBOL(ucb1x00_unregister_driver); | |
751 | ||
752 | MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>"); | |
753 | MODULE_DESCRIPTION("UCB1x00 core driver"); | |
754 | MODULE_LICENSE("GPL"); |