ezx-pcap.c 12 KB

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