ezx-pcap.c 12 KB

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