]>
Commit | Line | Data |
---|---|---|
13a09f93 DR |
1 | /* |
2 | * Driver for Motorola PCAP2 as present in EZX phones | |
3 | * | |
4 | * Copyright (C) 2006 Harald Welte <laforge@openezx.org> | |
5 | * Copyright (C) 2009 Daniel Ribeiro <drwyrm@gmail.com> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License version 2 as | |
9 | * published by the Free Software Foundation. | |
10 | * | |
11 | */ | |
12 | ||
13 | #include <linux/module.h> | |
14 | #include <linux/kernel.h> | |
15 | #include <linux/platform_device.h> | |
16 | #include <linux/interrupt.h> | |
17 | #include <linux/irq.h> | |
18 | #include <linux/mfd/ezx-pcap.h> | |
19 | #include <linux/spi/spi.h> | |
20 | ||
21 | #define PCAP_ADC_MAXQ 8 | |
22 | struct pcap_adc_request { | |
23 | u8 bank; | |
24 | u8 ch[2]; | |
25 | u32 flags; | |
26 | void (*callback)(void *, u16[]); | |
27 | void *data; | |
28 | }; | |
29 | ||
30 | struct pcap_adc_sync_request { | |
31 | u16 res[2]; | |
32 | struct completion completion; | |
33 | }; | |
34 | ||
35 | struct pcap_chip { | |
36 | struct spi_device *spi; | |
37 | ||
38 | /* IO */ | |
39 | u32 buf; | |
40 | struct mutex io_mutex; | |
41 | ||
42 | /* IRQ */ | |
43 | unsigned int irq_base; | |
44 | u32 msr; | |
45 | struct work_struct isr_work; | |
46 | struct work_struct msr_work; | |
47 | struct workqueue_struct *workqueue; | |
48 | ||
49 | /* ADC */ | |
50 | struct pcap_adc_request *adc_queue[PCAP_ADC_MAXQ]; | |
51 | u8 adc_head; | |
52 | u8 adc_tail; | |
53 | struct mutex adc_mutex; | |
54 | }; | |
55 | ||
56 | /* IO */ | |
57 | static int ezx_pcap_putget(struct pcap_chip *pcap, u32 *data) | |
58 | { | |
59 | struct spi_transfer t; | |
60 | struct spi_message m; | |
61 | int status; | |
62 | ||
63 | memset(&t, 0, sizeof t); | |
64 | spi_message_init(&m); | |
65 | t.len = sizeof(u32); | |
66 | spi_message_add_tail(&t, &m); | |
67 | ||
68 | pcap->buf = *data; | |
69 | t.tx_buf = (u8 *) &pcap->buf; | |
70 | t.rx_buf = (u8 *) &pcap->buf; | |
71 | status = spi_sync(pcap->spi, &m); | |
72 | ||
73 | if (status == 0) | |
74 | *data = pcap->buf; | |
75 | ||
76 | return status; | |
77 | } | |
78 | ||
79 | int ezx_pcap_write(struct pcap_chip *pcap, u8 reg_num, u32 value) | |
80 | { | |
81 | int ret; | |
82 | ||
83 | mutex_lock(&pcap->io_mutex); | |
84 | value &= PCAP_REGISTER_VALUE_MASK; | |
85 | value |= PCAP_REGISTER_WRITE_OP_BIT | |
86 | | (reg_num << PCAP_REGISTER_ADDRESS_SHIFT); | |
87 | ret = ezx_pcap_putget(pcap, &value); | |
88 | mutex_unlock(&pcap->io_mutex); | |
89 | ||
90 | return ret; | |
91 | } | |
92 | EXPORT_SYMBOL_GPL(ezx_pcap_write); | |
93 | ||
94 | int ezx_pcap_read(struct pcap_chip *pcap, u8 reg_num, u32 *value) | |
95 | { | |
96 | int ret; | |
97 | ||
98 | mutex_lock(&pcap->io_mutex); | |
99 | *value = PCAP_REGISTER_READ_OP_BIT | |
100 | | (reg_num << PCAP_REGISTER_ADDRESS_SHIFT); | |
101 | ||
102 | ret = ezx_pcap_putget(pcap, value); | |
103 | mutex_unlock(&pcap->io_mutex); | |
104 | ||
105 | return ret; | |
106 | } | |
107 | EXPORT_SYMBOL_GPL(ezx_pcap_read); | |
108 | ||
109 | /* IRQ */ | |
9f7b07d6 | 110 | int irq_to_pcap(struct pcap_chip *pcap, int irq) |
13a09f93 | 111 | { |
9f7b07d6 | 112 | return irq - pcap->irq_base; |
13a09f93 | 113 | } |
9f7b07d6 | 114 | EXPORT_SYMBOL_GPL(irq_to_pcap); |
13a09f93 DR |
115 | |
116 | int pcap_to_irq(struct pcap_chip *pcap, int irq) | |
117 | { | |
118 | return pcap->irq_base + irq; | |
119 | } | |
120 | EXPORT_SYMBOL_GPL(pcap_to_irq); | |
121 | ||
122 | static void pcap_mask_irq(unsigned int irq) | |
123 | { | |
124 | struct pcap_chip *pcap = get_irq_chip_data(irq); | |
125 | ||
9f7b07d6 | 126 | pcap->msr |= 1 << irq_to_pcap(pcap, irq); |
13a09f93 DR |
127 | queue_work(pcap->workqueue, &pcap->msr_work); |
128 | } | |
129 | ||
130 | static void pcap_unmask_irq(unsigned int irq) | |
131 | { | |
132 | struct pcap_chip *pcap = get_irq_chip_data(irq); | |
133 | ||
9f7b07d6 | 134 | pcap->msr &= ~(1 << irq_to_pcap(pcap, irq)); |
13a09f93 DR |
135 | queue_work(pcap->workqueue, &pcap->msr_work); |
136 | } | |
137 | ||
138 | static struct irq_chip pcap_irq_chip = { | |
139 | .name = "pcap", | |
140 | .mask = pcap_mask_irq, | |
141 | .unmask = pcap_unmask_irq, | |
142 | }; | |
143 | ||
144 | static void pcap_msr_work(struct work_struct *work) | |
145 | { | |
146 | struct pcap_chip *pcap = container_of(work, struct pcap_chip, msr_work); | |
147 | ||
148 | ezx_pcap_write(pcap, PCAP_REG_MSR, pcap->msr); | |
149 | } | |
150 | ||
151 | static void pcap_isr_work(struct work_struct *work) | |
152 | { | |
153 | struct pcap_chip *pcap = container_of(work, struct pcap_chip, isr_work); | |
154 | struct pcap_platform_data *pdata = pcap->spi->dev.platform_data; | |
155 | u32 msr, isr, int_sel, service; | |
156 | int irq; | |
157 | ||
158 | ezx_pcap_read(pcap, PCAP_REG_MSR, &msr); | |
159 | ezx_pcap_read(pcap, PCAP_REG_ISR, &isr); | |
160 | ||
161 | /* We cant service/ack irqs that are assigned to port 2 */ | |
162 | if (!(pdata->config & PCAP_SECOND_PORT)) { | |
163 | ezx_pcap_read(pcap, PCAP_REG_INT_SEL, &int_sel); | |
164 | isr &= ~int_sel; | |
165 | } | |
166 | ezx_pcap_write(pcap, PCAP_REG_ISR, isr); | |
167 | ||
168 | local_irq_disable(); | |
169 | service = isr & ~msr; | |
170 | ||
171 | for (irq = pcap->irq_base; service; service >>= 1, irq++) { | |
172 | if (service & 1) { | |
173 | struct irq_desc *desc = irq_to_desc(irq); | |
174 | ||
175 | if (WARN(!desc, KERN_WARNING | |
176 | "Invalid PCAP IRQ %d\n", irq)) | |
177 | break; | |
178 | ||
179 | if (desc->status & IRQ_DISABLED) | |
180 | note_interrupt(irq, desc, IRQ_NONE); | |
181 | else | |
182 | desc->handle_irq(irq, desc); | |
183 | } | |
184 | } | |
185 | local_irq_enable(); | |
186 | } | |
187 | ||
188 | static void pcap_irq_handler(unsigned int irq, struct irq_desc *desc) | |
189 | { | |
190 | struct pcap_chip *pcap = get_irq_data(irq); | |
191 | ||
192 | desc->chip->ack(irq); | |
193 | queue_work(pcap->workqueue, &pcap->isr_work); | |
194 | return; | |
195 | } | |
196 | ||
197 | /* ADC */ | |
198 | static void pcap_disable_adc(struct pcap_chip *pcap) | |
199 | { | |
200 | u32 tmp; | |
201 | ||
202 | ezx_pcap_read(pcap, PCAP_REG_ADC, &tmp); | |
203 | tmp &= ~(PCAP_ADC_ADEN|PCAP_ADC_BATT_I_ADC|PCAP_ADC_BATT_I_POLARITY); | |
204 | ezx_pcap_write(pcap, PCAP_REG_ADC, tmp); | |
205 | } | |
206 | ||
207 | static void pcap_adc_trigger(struct pcap_chip *pcap) | |
208 | { | |
209 | u32 tmp; | |
210 | u8 head; | |
211 | ||
212 | mutex_lock(&pcap->adc_mutex); | |
213 | head = pcap->adc_head; | |
214 | if (!pcap->adc_queue[head]) { | |
215 | /* queue is empty, save power */ | |
216 | pcap_disable_adc(pcap); | |
217 | mutex_unlock(&pcap->adc_mutex); | |
218 | return; | |
219 | } | |
220 | mutex_unlock(&pcap->adc_mutex); | |
221 | ||
222 | /* start conversion on requested bank */ | |
223 | tmp = pcap->adc_queue[head]->flags | PCAP_ADC_ADEN; | |
224 | ||
225 | if (pcap->adc_queue[head]->bank == PCAP_ADC_BANK_1) | |
226 | tmp |= PCAP_ADC_AD_SEL1; | |
227 | ||
228 | ezx_pcap_write(pcap, PCAP_REG_ADC, tmp); | |
229 | ezx_pcap_write(pcap, PCAP_REG_ADR, PCAP_ADR_ASC); | |
230 | } | |
231 | ||
232 | static irqreturn_t pcap_adc_irq(int irq, void *_pcap) | |
233 | { | |
234 | struct pcap_chip *pcap = _pcap; | |
235 | struct pcap_adc_request *req; | |
236 | u16 res[2]; | |
237 | u32 tmp; | |
238 | ||
239 | mutex_lock(&pcap->adc_mutex); | |
240 | req = pcap->adc_queue[pcap->adc_head]; | |
241 | ||
1c90ea2c DR |
242 | if (WARN(!req, KERN_WARNING "adc irq without pending request\n")) { |
243 | mutex_unlock(&pcap->adc_mutex); | |
13a09f93 | 244 | return IRQ_HANDLED; |
1c90ea2c | 245 | } |
13a09f93 DR |
246 | |
247 | /* read requested channels results */ | |
248 | ezx_pcap_read(pcap, PCAP_REG_ADC, &tmp); | |
249 | tmp &= ~(PCAP_ADC_ADA1_MASK | PCAP_ADC_ADA2_MASK); | |
250 | tmp |= (req->ch[0] << PCAP_ADC_ADA1_SHIFT); | |
251 | tmp |= (req->ch[1] << PCAP_ADC_ADA2_SHIFT); | |
252 | ezx_pcap_write(pcap, PCAP_REG_ADC, tmp); | |
253 | ezx_pcap_read(pcap, PCAP_REG_ADR, &tmp); | |
254 | res[0] = (tmp & PCAP_ADR_ADD1_MASK) >> PCAP_ADR_ADD1_SHIFT; | |
255 | res[1] = (tmp & PCAP_ADR_ADD2_MASK) >> PCAP_ADR_ADD2_SHIFT; | |
256 | ||
257 | pcap->adc_queue[pcap->adc_head] = NULL; | |
258 | pcap->adc_head = (pcap->adc_head + 1) & (PCAP_ADC_MAXQ - 1); | |
259 | mutex_unlock(&pcap->adc_mutex); | |
260 | ||
261 | /* pass the results and release memory */ | |
262 | req->callback(req->data, res); | |
263 | kfree(req); | |
264 | ||
265 | /* trigger next conversion (if any) on queue */ | |
266 | pcap_adc_trigger(pcap); | |
267 | ||
268 | return IRQ_HANDLED; | |
269 | } | |
270 | ||
271 | int pcap_adc_async(struct pcap_chip *pcap, u8 bank, u32 flags, u8 ch[], | |
272 | void *callback, void *data) | |
273 | { | |
274 | struct pcap_adc_request *req; | |
275 | ||
276 | /* This will be freed after we have a result */ | |
277 | req = kmalloc(sizeof(struct pcap_adc_request), GFP_KERNEL); | |
278 | if (!req) | |
279 | return -ENOMEM; | |
280 | ||
281 | req->bank = bank; | |
282 | req->flags = flags; | |
283 | req->ch[0] = ch[0]; | |
284 | req->ch[1] = ch[1]; | |
285 | req->callback = callback; | |
286 | req->data = data; | |
287 | ||
288 | mutex_lock(&pcap->adc_mutex); | |
289 | if (pcap->adc_queue[pcap->adc_tail]) { | |
290 | mutex_unlock(&pcap->adc_mutex); | |
291 | kfree(req); | |
292 | return -EBUSY; | |
293 | } | |
294 | pcap->adc_queue[pcap->adc_tail] = req; | |
295 | pcap->adc_tail = (pcap->adc_tail + 1) & (PCAP_ADC_MAXQ - 1); | |
296 | mutex_unlock(&pcap->adc_mutex); | |
297 | ||
298 | /* start conversion */ | |
299 | pcap_adc_trigger(pcap); | |
300 | ||
301 | return 0; | |
302 | } | |
303 | EXPORT_SYMBOL_GPL(pcap_adc_async); | |
304 | ||
305 | static void pcap_adc_sync_cb(void *param, u16 res[]) | |
306 | { | |
307 | struct pcap_adc_sync_request *req = param; | |
308 | ||
309 | req->res[0] = res[0]; | |
310 | req->res[1] = res[1]; | |
311 | complete(&req->completion); | |
312 | } | |
313 | ||
314 | int pcap_adc_sync(struct pcap_chip *pcap, u8 bank, u32 flags, u8 ch[], | |
315 | u16 res[]) | |
316 | { | |
317 | struct pcap_adc_sync_request sync_data; | |
318 | int ret; | |
319 | ||
320 | init_completion(&sync_data.completion); | |
321 | ret = pcap_adc_async(pcap, bank, flags, ch, pcap_adc_sync_cb, | |
322 | &sync_data); | |
323 | if (ret) | |
324 | return ret; | |
325 | wait_for_completion(&sync_data.completion); | |
326 | res[0] = sync_data.res[0]; | |
327 | res[1] = sync_data.res[1]; | |
328 | ||
329 | return 0; | |
330 | } | |
331 | EXPORT_SYMBOL_GPL(pcap_adc_sync); | |
332 | ||
333 | /* subdevs */ | |
334 | static int pcap_remove_subdev(struct device *dev, void *unused) | |
335 | { | |
336 | platform_device_unregister(to_platform_device(dev)); | |
337 | return 0; | |
338 | } | |
339 | ||
340 | static int __devinit pcap_add_subdev(struct pcap_chip *pcap, | |
341 | struct pcap_subdev *subdev) | |
342 | { | |
343 | struct platform_device *pdev; | |
344 | ||
345 | pdev = platform_device_alloc(subdev->name, subdev->id); | |
346 | pdev->dev.parent = &pcap->spi->dev; | |
347 | pdev->dev.platform_data = subdev->platform_data; | |
348 | platform_set_drvdata(pdev, pcap); | |
349 | ||
350 | return platform_device_add(pdev); | |
351 | } | |
352 | ||
353 | static int __devexit ezx_pcap_remove(struct spi_device *spi) | |
354 | { | |
355 | struct pcap_chip *pcap = dev_get_drvdata(&spi->dev); | |
356 | struct pcap_platform_data *pdata = spi->dev.platform_data; | |
357 | int i, adc_irq; | |
358 | ||
359 | /* remove all registered subdevs */ | |
360 | device_for_each_child(&spi->dev, NULL, pcap_remove_subdev); | |
361 | ||
362 | /* cleanup ADC */ | |
363 | adc_irq = pcap_to_irq(pcap, (pdata->config & PCAP_SECOND_PORT) ? | |
364 | PCAP_IRQ_ADCDONE2 : PCAP_IRQ_ADCDONE); | |
365 | free_irq(adc_irq, pcap); | |
366 | mutex_lock(&pcap->adc_mutex); | |
367 | for (i = 0; i < PCAP_ADC_MAXQ; i++) | |
368 | kfree(pcap->adc_queue[i]); | |
369 | mutex_unlock(&pcap->adc_mutex); | |
370 | ||
371 | /* cleanup irqchip */ | |
372 | for (i = pcap->irq_base; i < (pcap->irq_base + PCAP_NIRQS); i++) | |
373 | set_irq_chip_and_handler(i, NULL, NULL); | |
374 | ||
375 | destroy_workqueue(pcap->workqueue); | |
376 | ||
377 | kfree(pcap); | |
378 | ||
379 | return 0; | |
380 | } | |
381 | ||
382 | static int __devinit ezx_pcap_probe(struct spi_device *spi) | |
383 | { | |
384 | struct pcap_platform_data *pdata = spi->dev.platform_data; | |
385 | struct pcap_chip *pcap; | |
386 | int i, adc_irq; | |
387 | int ret = -ENODEV; | |
388 | ||
389 | /* platform data is required */ | |
390 | if (!pdata) | |
391 | goto ret; | |
392 | ||
393 | pcap = kzalloc(sizeof(*pcap), GFP_KERNEL); | |
394 | if (!pcap) { | |
395 | ret = -ENOMEM; | |
396 | goto ret; | |
397 | } | |
398 | ||
399 | mutex_init(&pcap->io_mutex); | |
400 | mutex_init(&pcap->adc_mutex); | |
401 | INIT_WORK(&pcap->isr_work, pcap_isr_work); | |
402 | INIT_WORK(&pcap->msr_work, pcap_msr_work); | |
403 | dev_set_drvdata(&spi->dev, pcap); | |
404 | ||
405 | /* setup spi */ | |
406 | spi->bits_per_word = 32; | |
407 | spi->mode = SPI_MODE_0 | (pdata->config & PCAP_CS_AH ? SPI_CS_HIGH : 0); | |
408 | ret = spi_setup(spi); | |
409 | if (ret) | |
410 | goto free_pcap; | |
411 | ||
412 | pcap->spi = spi; | |
413 | ||
414 | /* setup irq */ | |
415 | pcap->irq_base = pdata->irq_base; | |
416 | pcap->workqueue = create_singlethread_workqueue("pcapd"); | |
417 | if (!pcap->workqueue) { | |
418 | dev_err(&spi->dev, "cant create pcap thread\n"); | |
419 | goto free_pcap; | |
420 | } | |
421 | ||
422 | /* redirect interrupts to AP, except adcdone2 */ | |
423 | if (!(pdata->config & PCAP_SECOND_PORT)) | |
424 | ezx_pcap_write(pcap, PCAP_REG_INT_SEL, | |
425 | (1 << PCAP_IRQ_ADCDONE2)); | |
426 | ||
427 | /* setup irq chip */ | |
428 | for (i = pcap->irq_base; i < (pcap->irq_base + PCAP_NIRQS); i++) { | |
429 | set_irq_chip_and_handler(i, &pcap_irq_chip, handle_simple_irq); | |
430 | set_irq_chip_data(i, pcap); | |
431 | #ifdef CONFIG_ARM | |
432 | set_irq_flags(i, IRQF_VALID); | |
433 | #else | |
434 | set_irq_noprobe(i); | |
435 | #endif | |
436 | } | |
437 | ||
438 | /* mask/ack all PCAP interrupts */ | |
439 | ezx_pcap_write(pcap, PCAP_REG_MSR, PCAP_MASK_ALL_INTERRUPT); | |
440 | ezx_pcap_write(pcap, PCAP_REG_ISR, PCAP_CLEAR_INTERRUPT_REGISTER); | |
441 | pcap->msr = PCAP_MASK_ALL_INTERRUPT; | |
442 | ||
443 | set_irq_type(spi->irq, IRQ_TYPE_EDGE_RISING); | |
444 | set_irq_data(spi->irq, pcap); | |
445 | set_irq_chained_handler(spi->irq, pcap_irq_handler); | |
446 | set_irq_wake(spi->irq, 1); | |
447 | ||
448 | /* ADC */ | |
449 | adc_irq = pcap_to_irq(pcap, (pdata->config & PCAP_SECOND_PORT) ? | |
450 | PCAP_IRQ_ADCDONE2 : PCAP_IRQ_ADCDONE); | |
451 | ||
452 | ret = request_irq(adc_irq, pcap_adc_irq, 0, "ADC", pcap); | |
453 | if (ret) | |
454 | goto free_irqchip; | |
455 | ||
456 | /* setup subdevs */ | |
457 | for (i = 0; i < pdata->num_subdevs; i++) { | |
458 | ret = pcap_add_subdev(pcap, &pdata->subdevs[i]); | |
459 | if (ret) | |
460 | goto remove_subdevs; | |
461 | } | |
462 | ||
463 | /* board specific quirks */ | |
464 | if (pdata->init) | |
465 | pdata->init(pcap); | |
466 | ||
467 | return 0; | |
468 | ||
469 | remove_subdevs: | |
470 | device_for_each_child(&spi->dev, NULL, pcap_remove_subdev); | |
471 | /* free_adc: */ | |
472 | free_irq(adc_irq, pcap); | |
473 | free_irqchip: | |
474 | for (i = pcap->irq_base; i < (pcap->irq_base + PCAP_NIRQS); i++) | |
475 | set_irq_chip_and_handler(i, NULL, NULL); | |
476 | /* destroy_workqueue: */ | |
477 | destroy_workqueue(pcap->workqueue); | |
478 | free_pcap: | |
479 | kfree(pcap); | |
480 | ret: | |
481 | return ret; | |
482 | } | |
483 | ||
484 | static struct spi_driver ezxpcap_driver = { | |
485 | .probe = ezx_pcap_probe, | |
486 | .remove = __devexit_p(ezx_pcap_remove), | |
487 | .driver = { | |
488 | .name = "ezx-pcap", | |
489 | .owner = THIS_MODULE, | |
490 | }, | |
491 | }; | |
492 | ||
493 | static int __init ezx_pcap_init(void) | |
494 | { | |
495 | return spi_register_driver(&ezxpcap_driver); | |
496 | } | |
497 | ||
498 | static void __exit ezx_pcap_exit(void) | |
499 | { | |
500 | spi_unregister_driver(&ezxpcap_driver); | |
501 | } | |
502 | ||
503 | module_init(ezx_pcap_init); | |
504 | module_exit(ezx_pcap_exit); | |
505 | ||
506 | MODULE_LICENSE("GPL"); | |
507 | MODULE_AUTHOR("Daniel Ribeiro / Harald Welte"); | |
508 | MODULE_DESCRIPTION("Motorola PCAP2 ASIC Driver"); |