ezx-pcap.c 12 KB

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