ezx-pcap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. mutex_unlock(&pcap->adc_mutex);
  196. return IRQ_HANDLED;
  197. }
  198. /* read requested channels results */
  199. ezx_pcap_read(pcap, PCAP_REG_ADC, &tmp);
  200. tmp &= ~(PCAP_ADC_ADA1_MASK | PCAP_ADC_ADA2_MASK);
  201. tmp |= (req->ch[0] << PCAP_ADC_ADA1_SHIFT);
  202. tmp |= (req->ch[1] << PCAP_ADC_ADA2_SHIFT);
  203. ezx_pcap_write(pcap, PCAP_REG_ADC, tmp);
  204. ezx_pcap_read(pcap, PCAP_REG_ADR, &tmp);
  205. res[0] = (tmp & PCAP_ADR_ADD1_MASK) >> PCAP_ADR_ADD1_SHIFT;
  206. res[1] = (tmp & PCAP_ADR_ADD2_MASK) >> PCAP_ADR_ADD2_SHIFT;
  207. pcap->adc_queue[pcap->adc_head] = NULL;
  208. pcap->adc_head = (pcap->adc_head + 1) & (PCAP_ADC_MAXQ - 1);
  209. mutex_unlock(&pcap->adc_mutex);
  210. /* pass the results and release memory */
  211. req->callback(req->data, res);
  212. kfree(req);
  213. /* trigger next conversion (if any) on queue */
  214. pcap_adc_trigger(pcap);
  215. return IRQ_HANDLED;
  216. }
  217. int pcap_adc_async(struct pcap_chip *pcap, u8 bank, u32 flags, u8 ch[],
  218. void *callback, void *data)
  219. {
  220. struct pcap_adc_request *req;
  221. /* This will be freed after we have a result */
  222. req = kmalloc(sizeof(struct pcap_adc_request), GFP_KERNEL);
  223. if (!req)
  224. return -ENOMEM;
  225. req->bank = bank;
  226. req->flags = flags;
  227. req->ch[0] = ch[0];
  228. req->ch[1] = ch[1];
  229. req->callback = callback;
  230. req->data = data;
  231. mutex_lock(&pcap->adc_mutex);
  232. if (pcap->adc_queue[pcap->adc_tail]) {
  233. mutex_unlock(&pcap->adc_mutex);
  234. kfree(req);
  235. return -EBUSY;
  236. }
  237. pcap->adc_queue[pcap->adc_tail] = req;
  238. pcap->adc_tail = (pcap->adc_tail + 1) & (PCAP_ADC_MAXQ - 1);
  239. mutex_unlock(&pcap->adc_mutex);
  240. /* start conversion */
  241. pcap_adc_trigger(pcap);
  242. return 0;
  243. }
  244. EXPORT_SYMBOL_GPL(pcap_adc_async);
  245. static void pcap_adc_sync_cb(void *param, u16 res[])
  246. {
  247. struct pcap_adc_sync_request *req = param;
  248. req->res[0] = res[0];
  249. req->res[1] = res[1];
  250. complete(&req->completion);
  251. }
  252. int pcap_adc_sync(struct pcap_chip *pcap, u8 bank, u32 flags, u8 ch[],
  253. u16 res[])
  254. {
  255. struct pcap_adc_sync_request sync_data;
  256. int ret;
  257. init_completion(&sync_data.completion);
  258. ret = pcap_adc_async(pcap, bank, flags, ch, pcap_adc_sync_cb,
  259. &sync_data);
  260. if (ret)
  261. return ret;
  262. wait_for_completion(&sync_data.completion);
  263. res[0] = sync_data.res[0];
  264. res[1] = sync_data.res[1];
  265. return 0;
  266. }
  267. EXPORT_SYMBOL_GPL(pcap_adc_sync);
  268. /* subdevs */
  269. static int pcap_remove_subdev(struct device *dev, void *unused)
  270. {
  271. platform_device_unregister(to_platform_device(dev));
  272. return 0;
  273. }
  274. static int __devinit pcap_add_subdev(struct pcap_chip *pcap,
  275. struct pcap_subdev *subdev)
  276. {
  277. struct platform_device *pdev;
  278. pdev = platform_device_alloc(subdev->name, subdev->id);
  279. pdev->dev.parent = &pcap->spi->dev;
  280. pdev->dev.platform_data = subdev->platform_data;
  281. platform_set_drvdata(pdev, pcap);
  282. return platform_device_add(pdev);
  283. }
  284. static int __devexit ezx_pcap_remove(struct spi_device *spi)
  285. {
  286. struct pcap_chip *pcap = dev_get_drvdata(&spi->dev);
  287. struct pcap_platform_data *pdata = spi->dev.platform_data;
  288. int i, adc_irq;
  289. /* remove all registered subdevs */
  290. device_for_each_child(&spi->dev, NULL, pcap_remove_subdev);
  291. /* cleanup ADC */
  292. adc_irq = pcap_to_irq(pcap, (pdata->config & PCAP_SECOND_PORT) ?
  293. PCAP_IRQ_ADCDONE2 : PCAP_IRQ_ADCDONE);
  294. free_irq(adc_irq, pcap);
  295. mutex_lock(&pcap->adc_mutex);
  296. for (i = 0; i < PCAP_ADC_MAXQ; i++)
  297. kfree(pcap->adc_queue[i]);
  298. mutex_unlock(&pcap->adc_mutex);
  299. /* cleanup irqchip */
  300. for (i = pcap->irq_base; i < (pcap->irq_base + PCAP_NIRQS); i++)
  301. set_irq_chip_and_handler(i, NULL, NULL);
  302. destroy_workqueue(pcap->workqueue);
  303. kfree(pcap);
  304. return 0;
  305. }
  306. static int __devinit ezx_pcap_probe(struct spi_device *spi)
  307. {
  308. struct pcap_platform_data *pdata = spi->dev.platform_data;
  309. struct pcap_chip *pcap;
  310. int i, adc_irq;
  311. int ret = -ENODEV;
  312. /* platform data is required */
  313. if (!pdata)
  314. goto ret;
  315. pcap = kzalloc(sizeof(*pcap), GFP_KERNEL);
  316. if (!pcap) {
  317. ret = -ENOMEM;
  318. goto ret;
  319. }
  320. mutex_init(&pcap->io_mutex);
  321. mutex_init(&pcap->adc_mutex);
  322. INIT_WORK(&pcap->isr_work, pcap_isr_work);
  323. INIT_WORK(&pcap->msr_work, pcap_msr_work);
  324. dev_set_drvdata(&spi->dev, pcap);
  325. /* setup spi */
  326. spi->bits_per_word = 32;
  327. spi->mode = SPI_MODE_0 | (pdata->config & PCAP_CS_AH ? SPI_CS_HIGH : 0);
  328. ret = spi_setup(spi);
  329. if (ret)
  330. goto free_pcap;
  331. pcap->spi = spi;
  332. /* setup irq */
  333. pcap->irq_base = pdata->irq_base;
  334. pcap->workqueue = create_singlethread_workqueue("pcapd");
  335. if (!pcap->workqueue) {
  336. dev_err(&spi->dev, "cant create pcap thread\n");
  337. goto free_pcap;
  338. }
  339. /* redirect interrupts to AP, except adcdone2 */
  340. if (!(pdata->config & PCAP_SECOND_PORT))
  341. ezx_pcap_write(pcap, PCAP_REG_INT_SEL,
  342. (1 << PCAP_IRQ_ADCDONE2));
  343. /* setup irq chip */
  344. for (i = pcap->irq_base; i < (pcap->irq_base + PCAP_NIRQS); i++) {
  345. set_irq_chip_and_handler(i, &pcap_irq_chip, handle_simple_irq);
  346. set_irq_chip_data(i, pcap);
  347. #ifdef CONFIG_ARM
  348. set_irq_flags(i, IRQF_VALID);
  349. #else
  350. set_irq_noprobe(i);
  351. #endif
  352. }
  353. /* mask/ack all PCAP interrupts */
  354. ezx_pcap_write(pcap, PCAP_REG_MSR, PCAP_MASK_ALL_INTERRUPT);
  355. ezx_pcap_write(pcap, PCAP_REG_ISR, PCAP_CLEAR_INTERRUPT_REGISTER);
  356. pcap->msr = PCAP_MASK_ALL_INTERRUPT;
  357. set_irq_type(spi->irq, IRQ_TYPE_EDGE_RISING);
  358. set_irq_data(spi->irq, pcap);
  359. set_irq_chained_handler(spi->irq, pcap_irq_handler);
  360. set_irq_wake(spi->irq, 1);
  361. /* ADC */
  362. adc_irq = pcap_to_irq(pcap, (pdata->config & PCAP_SECOND_PORT) ?
  363. PCAP_IRQ_ADCDONE2 : PCAP_IRQ_ADCDONE);
  364. ret = request_irq(adc_irq, pcap_adc_irq, 0, "ADC", pcap);
  365. if (ret)
  366. goto free_irqchip;
  367. /* setup subdevs */
  368. for (i = 0; i < pdata->num_subdevs; i++) {
  369. ret = pcap_add_subdev(pcap, &pdata->subdevs[i]);
  370. if (ret)
  371. goto remove_subdevs;
  372. }
  373. /* board specific quirks */
  374. if (pdata->init)
  375. pdata->init(pcap);
  376. return 0;
  377. remove_subdevs:
  378. device_for_each_child(&spi->dev, NULL, pcap_remove_subdev);
  379. /* free_adc: */
  380. free_irq(adc_irq, pcap);
  381. free_irqchip:
  382. for (i = pcap->irq_base; i < (pcap->irq_base + PCAP_NIRQS); i++)
  383. set_irq_chip_and_handler(i, NULL, NULL);
  384. /* destroy_workqueue: */
  385. destroy_workqueue(pcap->workqueue);
  386. free_pcap:
  387. kfree(pcap);
  388. ret:
  389. return ret;
  390. }
  391. static struct spi_driver ezxpcap_driver = {
  392. .probe = ezx_pcap_probe,
  393. .remove = __devexit_p(ezx_pcap_remove),
  394. .driver = {
  395. .name = "ezx-pcap",
  396. .owner = THIS_MODULE,
  397. },
  398. };
  399. static int __init ezx_pcap_init(void)
  400. {
  401. return spi_register_driver(&ezxpcap_driver);
  402. }
  403. static void __exit ezx_pcap_exit(void)
  404. {
  405. spi_unregister_driver(&ezxpcap_driver);
  406. }
  407. module_init(ezx_pcap_init);
  408. module_exit(ezx_pcap_exit);
  409. MODULE_LICENSE("GPL");
  410. MODULE_AUTHOR("Daniel Ribeiro / Harald Welte");
  411. MODULE_DESCRIPTION("Motorola PCAP2 ASIC Driver");