ezx-pcap.c 12 KB

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