ezx-pcap.c 13 KB

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