mc13xxx-core.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. /*
  2. * Copyright 2009-2010 Pengutronix
  3. * Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>
  4. *
  5. * loosely based on an earlier driver that has
  6. * Copyright 2009 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it under
  9. * the terms of the GNU General Public License version 2 as published by the
  10. * Free Software Foundation.
  11. */
  12. #include <linux/slab.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/mutex.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/spi/spi.h>
  18. #include <linux/mfd/core.h>
  19. #include <linux/mfd/mc13xxx.h>
  20. struct mc13xxx {
  21. struct spi_device *spidev;
  22. struct mutex lock;
  23. int irq;
  24. irq_handler_t irqhandler[MC13XXX_NUM_IRQ];
  25. void *irqdata[MC13XXX_NUM_IRQ];
  26. };
  27. struct mc13783 {
  28. struct mc13xxx mc13xxx;
  29. int adcflags;
  30. };
  31. struct mc13xxx *mc13783_to_mc13xxx(struct mc13783 *mc13783)
  32. {
  33. return &mc13783->mc13xxx;
  34. }
  35. EXPORT_SYMBOL(mc13783_to_mc13xxx);
  36. #define MC13XXX_IRQSTAT0 0
  37. #define MC13XXX_IRQSTAT0_ADCDONEI (1 << 0)
  38. #define MC13XXX_IRQSTAT0_ADCBISDONEI (1 << 1)
  39. #define MC13XXX_IRQSTAT0_TSI (1 << 2)
  40. #define MC13783_IRQSTAT0_WHIGHI (1 << 3)
  41. #define MC13783_IRQSTAT0_WLOWI (1 << 4)
  42. #define MC13XXX_IRQSTAT0_CHGDETI (1 << 6)
  43. #define MC13783_IRQSTAT0_CHGOVI (1 << 7)
  44. #define MC13XXX_IRQSTAT0_CHGREVI (1 << 8)
  45. #define MC13XXX_IRQSTAT0_CHGSHORTI (1 << 9)
  46. #define MC13XXX_IRQSTAT0_CCCVI (1 << 10)
  47. #define MC13XXX_IRQSTAT0_CHGCURRI (1 << 11)
  48. #define MC13XXX_IRQSTAT0_BPONI (1 << 12)
  49. #define MC13XXX_IRQSTAT0_LOBATLI (1 << 13)
  50. #define MC13XXX_IRQSTAT0_LOBATHI (1 << 14)
  51. #define MC13783_IRQSTAT0_UDPI (1 << 15)
  52. #define MC13783_IRQSTAT0_USBI (1 << 16)
  53. #define MC13783_IRQSTAT0_IDI (1 << 19)
  54. #define MC13783_IRQSTAT0_SE1I (1 << 21)
  55. #define MC13783_IRQSTAT0_CKDETI (1 << 22)
  56. #define MC13783_IRQSTAT0_UDMI (1 << 23)
  57. #define MC13XXX_IRQMASK0 1
  58. #define MC13XXX_IRQMASK0_ADCDONEM MC13XXX_IRQSTAT0_ADCDONEI
  59. #define MC13XXX_IRQMASK0_ADCBISDONEM MC13XXX_IRQSTAT0_ADCBISDONEI
  60. #define MC13XXX_IRQMASK0_TSM MC13XXX_IRQSTAT0_TSI
  61. #define MC13783_IRQMASK0_WHIGHM MC13783_IRQSTAT0_WHIGHI
  62. #define MC13783_IRQMASK0_WLOWM MC13783_IRQSTAT0_WLOWI
  63. #define MC13XXX_IRQMASK0_CHGDETM MC13XXX_IRQSTAT0_CHGDETI
  64. #define MC13783_IRQMASK0_CHGOVM MC13783_IRQSTAT0_CHGOVI
  65. #define MC13XXX_IRQMASK0_CHGREVM MC13XXX_IRQSTAT0_CHGREVI
  66. #define MC13XXX_IRQMASK0_CHGSHORTM MC13XXX_IRQSTAT0_CHGSHORTI
  67. #define MC13XXX_IRQMASK0_CCCVM MC13XXX_IRQSTAT0_CCCVI
  68. #define MC13XXX_IRQMASK0_CHGCURRM MC13XXX_IRQSTAT0_CHGCURRI
  69. #define MC13XXX_IRQMASK0_BPONM MC13XXX_IRQSTAT0_BPONI
  70. #define MC13XXX_IRQMASK0_LOBATLM MC13XXX_IRQSTAT0_LOBATLI
  71. #define MC13XXX_IRQMASK0_LOBATHM MC13XXX_IRQSTAT0_LOBATHI
  72. #define MC13783_IRQMASK0_UDPM MC13783_IRQSTAT0_UDPI
  73. #define MC13783_IRQMASK0_USBM MC13783_IRQSTAT0_USBI
  74. #define MC13783_IRQMASK0_IDM MC13783_IRQSTAT0_IDI
  75. #define MC13783_IRQMASK0_SE1M MC13783_IRQSTAT0_SE1I
  76. #define MC13783_IRQMASK0_CKDETM MC13783_IRQSTAT0_CKDETI
  77. #define MC13783_IRQMASK0_UDMM MC13783_IRQSTAT0_UDMI
  78. #define MC13XXX_IRQSTAT1 3
  79. #define MC13XXX_IRQSTAT1_1HZI (1 << 0)
  80. #define MC13XXX_IRQSTAT1_TODAI (1 << 1)
  81. #define MC13783_IRQSTAT1_ONOFD1I (1 << 3)
  82. #define MC13783_IRQSTAT1_ONOFD2I (1 << 4)
  83. #define MC13783_IRQSTAT1_ONOFD3I (1 << 5)
  84. #define MC13XXX_IRQSTAT1_SYSRSTI (1 << 6)
  85. #define MC13XXX_IRQSTAT1_RTCRSTI (1 << 7)
  86. #define MC13XXX_IRQSTAT1_PCI (1 << 8)
  87. #define MC13XXX_IRQSTAT1_WARMI (1 << 9)
  88. #define MC13XXX_IRQSTAT1_MEMHLDI (1 << 10)
  89. #define MC13783_IRQSTAT1_PWRRDYI (1 << 11)
  90. #define MC13XXX_IRQSTAT1_THWARNLI (1 << 12)
  91. #define MC13XXX_IRQSTAT1_THWARNHI (1 << 13)
  92. #define MC13XXX_IRQSTAT1_CLKI (1 << 14)
  93. #define MC13783_IRQSTAT1_SEMAFI (1 << 15)
  94. #define MC13783_IRQSTAT1_MC2BI (1 << 17)
  95. #define MC13783_IRQSTAT1_HSDETI (1 << 18)
  96. #define MC13783_IRQSTAT1_HSLI (1 << 19)
  97. #define MC13783_IRQSTAT1_ALSPTHI (1 << 20)
  98. #define MC13783_IRQSTAT1_AHSSHORTI (1 << 21)
  99. #define MC13XXX_IRQMASK1 4
  100. #define MC13XXX_IRQMASK1_1HZM MC13XXX_IRQSTAT1_1HZI
  101. #define MC13XXX_IRQMASK1_TODAM MC13XXX_IRQSTAT1_TODAI
  102. #define MC13783_IRQMASK1_ONOFD1M MC13783_IRQSTAT1_ONOFD1I
  103. #define MC13783_IRQMASK1_ONOFD2M MC13783_IRQSTAT1_ONOFD2I
  104. #define MC13783_IRQMASK1_ONOFD3M MC13783_IRQSTAT1_ONOFD3I
  105. #define MC13XXX_IRQMASK1_SYSRSTM MC13XXX_IRQSTAT1_SYSRSTI
  106. #define MC13XXX_IRQMASK1_RTCRSTM MC13XXX_IRQSTAT1_RTCRSTI
  107. #define MC13XXX_IRQMASK1_PCM MC13XXX_IRQSTAT1_PCI
  108. #define MC13XXX_IRQMASK1_WARMM MC13XXX_IRQSTAT1_WARMI
  109. #define MC13XXX_IRQMASK1_MEMHLDM MC13XXX_IRQSTAT1_MEMHLDI
  110. #define MC13783_IRQMASK1_PWRRDYM MC13783_IRQSTAT1_PWRRDYI
  111. #define MC13XXX_IRQMASK1_THWARNLM MC13XXX_IRQSTAT1_THWARNLI
  112. #define MC13XXX_IRQMASK1_THWARNHM MC13XXX_IRQSTAT1_THWARNHI
  113. #define MC13XXX_IRQMASK1_CLKM MC13XXX_IRQSTAT1_CLKI
  114. #define MC13783_IRQMASK1_SEMAFM MC13783_IRQSTAT1_SEMAFI
  115. #define MC13783_IRQMASK1_MC2BM MC13783_IRQSTAT1_MC2BI
  116. #define MC13783_IRQMASK1_HSDETM MC13783_IRQSTAT1_HSDETI
  117. #define MC13783_IRQMASK1_HSLM MC13783_IRQSTAT1_HSLI
  118. #define MC13783_IRQMASK1_ALSPTHM MC13783_IRQSTAT1_ALSPTHI
  119. #define MC13783_IRQMASK1_AHSSHORTM MC13783_IRQSTAT1_AHSSHORTI
  120. #define MC13XXX_REVISION 7
  121. #define MC13XXX_REVISION_REVMETAL (0x07 << 0)
  122. #define MC13XXX_REVISION_REVFULL (0x03 << 3)
  123. #define MC13XXX_REVISION_ICID (0x07 << 6)
  124. #define MC13XXX_REVISION_FIN (0x03 << 9)
  125. #define MC13XXX_REVISION_FAB (0x03 << 11)
  126. #define MC13XXX_REVISION_ICIDCODE (0x3f << 13)
  127. #define MC13783_ADC1 44
  128. #define MC13783_ADC1_ADEN (1 << 0)
  129. #define MC13783_ADC1_RAND (1 << 1)
  130. #define MC13783_ADC1_ADSEL (1 << 3)
  131. #define MC13783_ADC1_ASC (1 << 20)
  132. #define MC13783_ADC1_ADTRIGIGN (1 << 21)
  133. #define MC13783_ADC2 45
  134. #define MC13XXX_NUMREGS 0x3f
  135. void mc13xxx_lock(struct mc13xxx *mc13xxx)
  136. {
  137. if (!mutex_trylock(&mc13xxx->lock)) {
  138. dev_dbg(&mc13xxx->spidev->dev, "wait for %s from %pf\n",
  139. __func__, __builtin_return_address(0));
  140. mutex_lock(&mc13xxx->lock);
  141. }
  142. dev_dbg(&mc13xxx->spidev->dev, "%s from %pf\n",
  143. __func__, __builtin_return_address(0));
  144. }
  145. EXPORT_SYMBOL(mc13xxx_lock);
  146. void mc13xxx_unlock(struct mc13xxx *mc13xxx)
  147. {
  148. dev_dbg(&mc13xxx->spidev->dev, "%s from %pf\n",
  149. __func__, __builtin_return_address(0));
  150. mutex_unlock(&mc13xxx->lock);
  151. }
  152. EXPORT_SYMBOL(mc13xxx_unlock);
  153. #define MC13XXX_REGOFFSET_SHIFT 25
  154. int mc13xxx_reg_read(struct mc13xxx *mc13xxx, unsigned int offset, u32 *val)
  155. {
  156. struct spi_transfer t;
  157. struct spi_message m;
  158. int ret;
  159. BUG_ON(!mutex_is_locked(&mc13xxx->lock));
  160. if (offset > MC13XXX_NUMREGS)
  161. return -EINVAL;
  162. *val = offset << MC13XXX_REGOFFSET_SHIFT;
  163. memset(&t, 0, sizeof(t));
  164. t.tx_buf = val;
  165. t.rx_buf = val;
  166. t.len = sizeof(u32);
  167. spi_message_init(&m);
  168. spi_message_add_tail(&t, &m);
  169. ret = spi_sync(mc13xxx->spidev, &m);
  170. /* error in message.status implies error return from spi_sync */
  171. BUG_ON(!ret && m.status);
  172. if (ret)
  173. return ret;
  174. *val &= 0xffffff;
  175. dev_vdbg(&mc13xxx->spidev->dev, "[0x%02x] -> 0x%06x\n", offset, *val);
  176. return 0;
  177. }
  178. EXPORT_SYMBOL(mc13xxx_reg_read);
  179. int mc13xxx_reg_write(struct mc13xxx *mc13xxx, unsigned int offset, u32 val)
  180. {
  181. u32 buf;
  182. struct spi_transfer t;
  183. struct spi_message m;
  184. int ret;
  185. BUG_ON(!mutex_is_locked(&mc13xxx->lock));
  186. dev_vdbg(&mc13xxx->spidev->dev, "[0x%02x] <- 0x%06x\n", offset, val);
  187. if (offset > MC13XXX_NUMREGS || val > 0xffffff)
  188. return -EINVAL;
  189. buf = 1 << 31 | offset << MC13XXX_REGOFFSET_SHIFT | val;
  190. memset(&t, 0, sizeof(t));
  191. t.tx_buf = &buf;
  192. t.rx_buf = &buf;
  193. t.len = sizeof(u32);
  194. spi_message_init(&m);
  195. spi_message_add_tail(&t, &m);
  196. ret = spi_sync(mc13xxx->spidev, &m);
  197. BUG_ON(!ret && m.status);
  198. if (ret)
  199. return ret;
  200. return 0;
  201. }
  202. EXPORT_SYMBOL(mc13xxx_reg_write);
  203. int mc13xxx_reg_rmw(struct mc13xxx *mc13xxx, unsigned int offset,
  204. u32 mask, u32 val)
  205. {
  206. int ret;
  207. u32 valread;
  208. BUG_ON(val & ~mask);
  209. ret = mc13xxx_reg_read(mc13xxx, offset, &valread);
  210. if (ret)
  211. return ret;
  212. valread = (valread & ~mask) | val;
  213. return mc13xxx_reg_write(mc13xxx, offset, valread);
  214. }
  215. EXPORT_SYMBOL(mc13xxx_reg_rmw);
  216. int mc13xxx_irq_mask(struct mc13xxx *mc13xxx, int irq)
  217. {
  218. int ret;
  219. unsigned int offmask = irq < 24 ? MC13XXX_IRQMASK0 : MC13XXX_IRQMASK1;
  220. u32 irqbit = 1 << (irq < 24 ? irq : irq - 24);
  221. u32 mask;
  222. if (irq < 0 || irq >= MC13XXX_NUM_IRQ)
  223. return -EINVAL;
  224. ret = mc13xxx_reg_read(mc13xxx, offmask, &mask);
  225. if (ret)
  226. return ret;
  227. if (mask & irqbit)
  228. /* already masked */
  229. return 0;
  230. return mc13xxx_reg_write(mc13xxx, offmask, mask | irqbit);
  231. }
  232. EXPORT_SYMBOL(mc13xxx_irq_mask);
  233. int mc13xxx_irq_unmask(struct mc13xxx *mc13xxx, int irq)
  234. {
  235. int ret;
  236. unsigned int offmask = irq < 24 ? MC13XXX_IRQMASK0 : MC13XXX_IRQMASK1;
  237. u32 irqbit = 1 << (irq < 24 ? irq : irq - 24);
  238. u32 mask;
  239. if (irq < 0 || irq >= MC13XXX_NUM_IRQ)
  240. return -EINVAL;
  241. ret = mc13xxx_reg_read(mc13xxx, offmask, &mask);
  242. if (ret)
  243. return ret;
  244. if (!(mask & irqbit))
  245. /* already unmasked */
  246. return 0;
  247. return mc13xxx_reg_write(mc13xxx, offmask, mask & ~irqbit);
  248. }
  249. EXPORT_SYMBOL(mc13xxx_irq_unmask);
  250. int mc13xxx_irq_status(struct mc13xxx *mc13xxx, int irq,
  251. int *enabled, int *pending)
  252. {
  253. int ret;
  254. unsigned int offmask = irq < 24 ? MC13XXX_IRQMASK0 : MC13XXX_IRQMASK1;
  255. unsigned int offstat = irq < 24 ? MC13XXX_IRQSTAT0 : MC13XXX_IRQSTAT1;
  256. u32 irqbit = 1 << (irq < 24 ? irq : irq - 24);
  257. if (irq < 0 || irq >= MC13XXX_NUM_IRQ)
  258. return -EINVAL;
  259. if (enabled) {
  260. u32 mask;
  261. ret = mc13xxx_reg_read(mc13xxx, offmask, &mask);
  262. if (ret)
  263. return ret;
  264. *enabled = mask & irqbit;
  265. }
  266. if (pending) {
  267. u32 stat;
  268. ret = mc13xxx_reg_read(mc13xxx, offstat, &stat);
  269. if (ret)
  270. return ret;
  271. *pending = stat & irqbit;
  272. }
  273. return 0;
  274. }
  275. EXPORT_SYMBOL(mc13xxx_irq_status);
  276. int mc13xxx_irq_ack(struct mc13xxx *mc13xxx, int irq)
  277. {
  278. unsigned int offstat = irq < 24 ? MC13XXX_IRQSTAT0 : MC13XXX_IRQSTAT1;
  279. unsigned int val = 1 << (irq < 24 ? irq : irq - 24);
  280. BUG_ON(irq < 0 || irq >= MC13XXX_NUM_IRQ);
  281. return mc13xxx_reg_write(mc13xxx, offstat, val);
  282. }
  283. EXPORT_SYMBOL(mc13xxx_irq_ack);
  284. int mc13xxx_irq_request_nounmask(struct mc13xxx *mc13xxx, int irq,
  285. irq_handler_t handler, const char *name, void *dev)
  286. {
  287. BUG_ON(!mutex_is_locked(&mc13xxx->lock));
  288. BUG_ON(!handler);
  289. if (irq < 0 || irq >= MC13XXX_NUM_IRQ)
  290. return -EINVAL;
  291. if (mc13xxx->irqhandler[irq])
  292. return -EBUSY;
  293. mc13xxx->irqhandler[irq] = handler;
  294. mc13xxx->irqdata[irq] = dev;
  295. return 0;
  296. }
  297. EXPORT_SYMBOL(mc13xxx_irq_request_nounmask);
  298. int mc13xxx_irq_request(struct mc13xxx *mc13xxx, int irq,
  299. irq_handler_t handler, const char *name, void *dev)
  300. {
  301. int ret;
  302. ret = mc13xxx_irq_request_nounmask(mc13xxx, irq, handler, name, dev);
  303. if (ret)
  304. return ret;
  305. ret = mc13xxx_irq_unmask(mc13xxx, irq);
  306. if (ret) {
  307. mc13xxx->irqhandler[irq] = NULL;
  308. mc13xxx->irqdata[irq] = NULL;
  309. return ret;
  310. }
  311. return 0;
  312. }
  313. EXPORT_SYMBOL(mc13xxx_irq_request);
  314. int mc13xxx_irq_free(struct mc13xxx *mc13xxx, int irq, void *dev)
  315. {
  316. int ret;
  317. BUG_ON(!mutex_is_locked(&mc13xxx->lock));
  318. if (irq < 0 || irq >= MC13XXX_NUM_IRQ || !mc13xxx->irqhandler[irq] ||
  319. mc13xxx->irqdata[irq] != dev)
  320. return -EINVAL;
  321. ret = mc13xxx_irq_mask(mc13xxx, irq);
  322. if (ret)
  323. return ret;
  324. mc13xxx->irqhandler[irq] = NULL;
  325. mc13xxx->irqdata[irq] = NULL;
  326. return 0;
  327. }
  328. EXPORT_SYMBOL(mc13xxx_irq_free);
  329. static inline irqreturn_t mc13xxx_irqhandler(struct mc13xxx *mc13xxx, int irq)
  330. {
  331. return mc13xxx->irqhandler[irq](irq, mc13xxx->irqdata[irq]);
  332. }
  333. /*
  334. * returns: number of handled irqs or negative error
  335. * locking: holds mc13xxx->lock
  336. */
  337. static int mc13xxx_irq_handle(struct mc13xxx *mc13xxx,
  338. unsigned int offstat, unsigned int offmask, int baseirq)
  339. {
  340. u32 stat, mask;
  341. int ret = mc13xxx_reg_read(mc13xxx, offstat, &stat);
  342. int num_handled = 0;
  343. if (ret)
  344. return ret;
  345. ret = mc13xxx_reg_read(mc13xxx, offmask, &mask);
  346. if (ret)
  347. return ret;
  348. while (stat & ~mask) {
  349. int irq = __ffs(stat & ~mask);
  350. stat &= ~(1 << irq);
  351. if (likely(mc13xxx->irqhandler[baseirq + irq])) {
  352. irqreturn_t handled;
  353. handled = mc13xxx_irqhandler(mc13xxx, baseirq + irq);
  354. if (handled == IRQ_HANDLED)
  355. num_handled++;
  356. } else {
  357. dev_err(&mc13xxx->spidev->dev,
  358. "BUG: irq %u but no handler\n",
  359. baseirq + irq);
  360. mask |= 1 << irq;
  361. ret = mc13xxx_reg_write(mc13xxx, offmask, mask);
  362. }
  363. }
  364. return num_handled;
  365. }
  366. static irqreturn_t mc13xxx_irq_thread(int irq, void *data)
  367. {
  368. struct mc13xxx *mc13xxx = data;
  369. irqreturn_t ret;
  370. int handled = 0;
  371. mc13xxx_lock(mc13xxx);
  372. ret = mc13xxx_irq_handle(mc13xxx, MC13XXX_IRQSTAT0,
  373. MC13XXX_IRQMASK0, 0);
  374. if (ret > 0)
  375. handled = 1;
  376. ret = mc13xxx_irq_handle(mc13xxx, MC13XXX_IRQSTAT1,
  377. MC13XXX_IRQMASK1, 24);
  378. if (ret > 0)
  379. handled = 1;
  380. mc13xxx_unlock(mc13xxx);
  381. return IRQ_RETVAL(handled);
  382. }
  383. enum mc13xxx_id {
  384. MC13XXX_ID_MC13783,
  385. MC13XXX_ID_MC13892,
  386. MC13XXX_ID_INVALID,
  387. };
  388. const char *mc13xxx_chipname[] = {
  389. [MC13XXX_ID_MC13783] = "mc13783",
  390. [MC13XXX_ID_MC13892] = "mc13892",
  391. };
  392. #define maskval(reg, mask) (((reg) & (mask)) >> __ffs(mask))
  393. static int mc13xxx_identify(struct mc13xxx *mc13xxx, enum mc13xxx_id *id)
  394. {
  395. u32 icid;
  396. u32 revision;
  397. const char *name;
  398. int ret;
  399. ret = mc13xxx_reg_read(mc13xxx, 46, &icid);
  400. if (ret)
  401. return ret;
  402. icid = (icid >> 6) & 0x7;
  403. switch (icid) {
  404. case 2:
  405. *id = MC13XXX_ID_MC13783;
  406. name = "mc13783";
  407. break;
  408. case 7:
  409. *id = MC13XXX_ID_MC13892;
  410. name = "mc13892";
  411. break;
  412. default:
  413. *id = MC13XXX_ID_INVALID;
  414. break;
  415. }
  416. if (*id == MC13XXX_ID_MC13783 || *id == MC13XXX_ID_MC13892) {
  417. ret = mc13xxx_reg_read(mc13xxx, MC13XXX_REVISION, &revision);
  418. if (ret)
  419. return ret;
  420. dev_info(&mc13xxx->spidev->dev, "%s: rev: %d.%d, "
  421. "fin: %d, fab: %d, icid: %d/%d\n",
  422. mc13xxx_chipname[*id],
  423. maskval(revision, MC13XXX_REVISION_REVFULL),
  424. maskval(revision, MC13XXX_REVISION_REVMETAL),
  425. maskval(revision, MC13XXX_REVISION_FIN),
  426. maskval(revision, MC13XXX_REVISION_FAB),
  427. maskval(revision, MC13XXX_REVISION_ICID),
  428. maskval(revision, MC13XXX_REVISION_ICIDCODE));
  429. }
  430. if (*id != MC13XXX_ID_INVALID) {
  431. const struct spi_device_id *devid =
  432. spi_get_device_id(mc13xxx->spidev);
  433. if (!devid || devid->driver_data != *id)
  434. dev_warn(&mc13xxx->spidev->dev, "device id doesn't "
  435. "match auto detection!\n");
  436. }
  437. return 0;
  438. }
  439. static const char *mc13xxx_get_chipname(struct mc13xxx *mc13xxx)
  440. {
  441. const struct spi_device_id *devid =
  442. spi_get_device_id(mc13xxx->spidev);
  443. if (!devid)
  444. return NULL;
  445. return mc13xxx_chipname[devid->driver_data];
  446. }
  447. #include <linux/mfd/mc13783.h>
  448. int mc13xxx_get_flags(struct mc13xxx *mc13xxx)
  449. {
  450. struct mc13xxx_platform_data *pdata =
  451. dev_get_platdata(&mc13xxx->spidev->dev);
  452. return pdata->flags;
  453. }
  454. EXPORT_SYMBOL(mc13xxx_get_flags);
  455. #define MC13783_ADC1_CHAN0_SHIFT 5
  456. #define MC13783_ADC1_CHAN1_SHIFT 8
  457. struct mc13xxx_adcdone_data {
  458. struct mc13xxx *mc13xxx;
  459. struct completion done;
  460. };
  461. static irqreturn_t mc13783_handler_adcdone(int irq, void *data)
  462. {
  463. struct mc13xxx_adcdone_data *adcdone_data = data;
  464. mc13xxx_irq_ack(adcdone_data->mc13xxx, irq);
  465. complete_all(&adcdone_data->done);
  466. return IRQ_HANDLED;
  467. }
  468. #define MC13783_ADC_WORKING (1 << 0)
  469. int mc13783_adc_do_conversion(struct mc13783 *mc13783, unsigned int mode,
  470. unsigned int channel, unsigned int *sample)
  471. {
  472. struct mc13xxx *mc13xxx = &mc13783->mc13xxx;
  473. u32 adc0, adc1, old_adc0;
  474. int i, ret;
  475. struct mc13xxx_adcdone_data adcdone_data = {
  476. .mc13xxx = mc13xxx,
  477. };
  478. init_completion(&adcdone_data.done);
  479. dev_dbg(&mc13xxx->spidev->dev, "%s\n", __func__);
  480. mc13xxx_lock(mc13xxx);
  481. if (mc13783->adcflags & MC13783_ADC_WORKING) {
  482. ret = -EBUSY;
  483. goto out;
  484. }
  485. mc13783->adcflags |= MC13783_ADC_WORKING;
  486. mc13xxx_reg_read(mc13xxx, MC13783_ADC0, &old_adc0);
  487. adc0 = MC13783_ADC0_ADINC1 | MC13783_ADC0_ADINC2;
  488. adc1 = MC13783_ADC1_ADEN | MC13783_ADC1_ADTRIGIGN | MC13783_ADC1_ASC;
  489. if (channel > 7)
  490. adc1 |= MC13783_ADC1_ADSEL;
  491. switch (mode) {
  492. case MC13783_ADC_MODE_TS:
  493. adc0 |= MC13783_ADC0_ADREFEN | MC13783_ADC0_TSMOD0 |
  494. MC13783_ADC0_TSMOD1;
  495. adc1 |= 4 << MC13783_ADC1_CHAN1_SHIFT;
  496. break;
  497. case MC13783_ADC_MODE_SINGLE_CHAN:
  498. adc0 |= old_adc0 & MC13783_ADC0_TSMOD_MASK;
  499. adc1 |= (channel & 0x7) << MC13783_ADC1_CHAN0_SHIFT;
  500. adc1 |= MC13783_ADC1_RAND;
  501. break;
  502. case MC13783_ADC_MODE_MULT_CHAN:
  503. adc0 |= old_adc0 & MC13783_ADC0_TSMOD_MASK;
  504. adc1 |= 4 << MC13783_ADC1_CHAN1_SHIFT;
  505. break;
  506. default:
  507. mc13783_unlock(mc13783);
  508. return -EINVAL;
  509. }
  510. dev_dbg(&mc13783->mc13xxx.spidev->dev, "%s: request irq\n", __func__);
  511. mc13xxx_irq_request(mc13xxx, MC13783_IRQ_ADCDONE,
  512. mc13783_handler_adcdone, __func__, &adcdone_data);
  513. mc13xxx_irq_ack(mc13xxx, MC13783_IRQ_ADCDONE);
  514. mc13xxx_reg_write(mc13xxx, MC13783_ADC0, adc0);
  515. mc13xxx_reg_write(mc13xxx, MC13783_ADC1, adc1);
  516. mc13xxx_unlock(mc13xxx);
  517. ret = wait_for_completion_interruptible_timeout(&adcdone_data.done, HZ);
  518. if (!ret)
  519. ret = -ETIMEDOUT;
  520. mc13xxx_lock(mc13xxx);
  521. mc13xxx_irq_free(mc13xxx, MC13783_IRQ_ADCDONE, &adcdone_data);
  522. if (ret > 0)
  523. for (i = 0; i < 4; ++i) {
  524. ret = mc13xxx_reg_read(mc13xxx,
  525. MC13783_ADC2, &sample[i]);
  526. if (ret)
  527. break;
  528. }
  529. if (mode == MC13783_ADC_MODE_TS)
  530. /* restore TSMOD */
  531. mc13xxx_reg_write(mc13xxx, MC13783_ADC0, old_adc0);
  532. mc13783->adcflags &= ~MC13783_ADC_WORKING;
  533. out:
  534. mc13xxx_unlock(mc13xxx);
  535. return ret;
  536. }
  537. EXPORT_SYMBOL_GPL(mc13783_adc_do_conversion);
  538. static int mc13xxx_add_subdevice_pdata(struct mc13xxx *mc13xxx,
  539. const char *format, void *pdata, size_t pdata_size)
  540. {
  541. char buf[30];
  542. const char *name = mc13xxx_get_chipname(mc13xxx);
  543. struct mfd_cell cell = {
  544. .platform_data = pdata,
  545. .pdata_size = pdata_size,
  546. };
  547. /* there is no asnprintf in the kernel :-( */
  548. if (snprintf(buf, sizeof(buf), format, name) > sizeof(buf))
  549. return -E2BIG;
  550. cell.name = kmemdup(buf, strlen(buf) + 1, GFP_KERNEL);
  551. if (!cell.name)
  552. return -ENOMEM;
  553. return mfd_add_devices(&mc13xxx->spidev->dev, -1, &cell, 1, NULL, 0);
  554. }
  555. static int mc13xxx_add_subdevice(struct mc13xxx *mc13xxx, const char *format)
  556. {
  557. return mc13xxx_add_subdevice_pdata(mc13xxx, format, NULL, 0);
  558. }
  559. static int mc13xxx_probe(struct spi_device *spi)
  560. {
  561. struct mc13xxx *mc13xxx;
  562. struct mc13xxx_platform_data *pdata = dev_get_platdata(&spi->dev);
  563. enum mc13xxx_id id;
  564. int ret;
  565. mc13xxx = kzalloc(sizeof(*mc13xxx), GFP_KERNEL);
  566. if (!mc13xxx)
  567. return -ENOMEM;
  568. dev_set_drvdata(&spi->dev, mc13xxx);
  569. spi->mode = SPI_MODE_0 | SPI_CS_HIGH;
  570. spi->bits_per_word = 32;
  571. spi_setup(spi);
  572. mc13xxx->spidev = spi;
  573. mutex_init(&mc13xxx->lock);
  574. mc13xxx_lock(mc13xxx);
  575. ret = mc13xxx_identify(mc13xxx, &id);
  576. if (ret || id == MC13XXX_ID_INVALID)
  577. goto err_revision;
  578. /* mask all irqs */
  579. ret = mc13xxx_reg_write(mc13xxx, MC13XXX_IRQMASK0, 0x00ffffff);
  580. if (ret)
  581. goto err_mask;
  582. ret = mc13xxx_reg_write(mc13xxx, MC13XXX_IRQMASK1, 0x00ffffff);
  583. if (ret)
  584. goto err_mask;
  585. ret = request_threaded_irq(spi->irq, NULL, mc13xxx_irq_thread,
  586. IRQF_ONESHOT | IRQF_TRIGGER_HIGH, "mc13xxx", mc13xxx);
  587. if (ret) {
  588. err_mask:
  589. err_revision:
  590. mc13xxx_unlock(mc13xxx);
  591. dev_set_drvdata(&spi->dev, NULL);
  592. kfree(mc13xxx);
  593. return ret;
  594. }
  595. mc13xxx_unlock(mc13xxx);
  596. if (pdata->flags & MC13XXX_USE_ADC)
  597. mc13xxx_add_subdevice(mc13xxx, "%s-adc");
  598. if (pdata->flags & MC13XXX_USE_CODEC)
  599. mc13xxx_add_subdevice(mc13xxx, "%s-codec");
  600. if (pdata->flags & MC13XXX_USE_REGULATOR) {
  601. mc13xxx_add_subdevice_pdata(mc13xxx, "%s-regulator",
  602. &pdata->regulators, sizeof(pdata->regulators));
  603. }
  604. if (pdata->flags & MC13XXX_USE_RTC)
  605. mc13xxx_add_subdevice(mc13xxx, "%s-rtc");
  606. if (pdata->flags & MC13XXX_USE_TOUCHSCREEN)
  607. mc13xxx_add_subdevice(mc13xxx, "%s-ts");
  608. if (pdata->flags & MC13XXX_USE_LED)
  609. mc13xxx_add_subdevice_pdata(mc13xxx, "%s-led",
  610. pdata->leds, sizeof(*pdata->leds));
  611. return 0;
  612. }
  613. static int __devexit mc13xxx_remove(struct spi_device *spi)
  614. {
  615. struct mc13xxx *mc13xxx = dev_get_drvdata(&spi->dev);
  616. free_irq(mc13xxx->spidev->irq, mc13xxx);
  617. mfd_remove_devices(&spi->dev);
  618. kfree(mc13xxx);
  619. return 0;
  620. }
  621. static const struct spi_device_id mc13xxx_device_id[] = {
  622. {
  623. .name = "mc13783",
  624. .driver_data = MC13XXX_ID_MC13783,
  625. }, {
  626. .name = "mc13892",
  627. .driver_data = MC13XXX_ID_MC13892,
  628. }, {
  629. /* sentinel */
  630. }
  631. };
  632. MODULE_DEVICE_TABLE(spi, mc13xxx_device_id);
  633. static struct spi_driver mc13xxx_driver = {
  634. .id_table = mc13xxx_device_id,
  635. .driver = {
  636. .name = "mc13xxx",
  637. .bus = &spi_bus_type,
  638. .owner = THIS_MODULE,
  639. },
  640. .probe = mc13xxx_probe,
  641. .remove = __devexit_p(mc13xxx_remove),
  642. };
  643. static int __init mc13xxx_init(void)
  644. {
  645. return spi_register_driver(&mc13xxx_driver);
  646. }
  647. subsys_initcall(mc13xxx_init);
  648. static void __exit mc13xxx_exit(void)
  649. {
  650. spi_unregister_driver(&mc13xxx_driver);
  651. }
  652. module_exit(mc13xxx_exit);
  653. MODULE_DESCRIPTION("Core driver for Freescale MC13XXX PMIC");
  654. MODULE_AUTHOR("Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>");
  655. MODULE_LICENSE("GPL v2");