ezx-pcap.c 13 KB

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