ezx-pcap.c 12 KB

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