mc13xxx-core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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/module.h>
  13. #include <linux/of.h>
  14. #include <linux/of_device.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/mfd/core.h>
  17. #include "mc13xxx.h"
  18. #define MC13XXX_IRQSTAT0 0
  19. #define MC13XXX_IRQMASK0 1
  20. #define MC13XXX_IRQSTAT1 3
  21. #define MC13XXX_IRQMASK1 4
  22. #define MC13XXX_REVISION 7
  23. #define MC13XXX_REVISION_REVMETAL (0x07 << 0)
  24. #define MC13XXX_REVISION_REVFULL (0x03 << 3)
  25. #define MC13XXX_REVISION_ICID (0x07 << 6)
  26. #define MC13XXX_REVISION_FIN (0x03 << 9)
  27. #define MC13XXX_REVISION_FAB (0x03 << 11)
  28. #define MC13XXX_REVISION_ICIDCODE (0x3f << 13)
  29. #define MC34708_REVISION_REVMETAL (0x07 << 0)
  30. #define MC34708_REVISION_REVFULL (0x07 << 3)
  31. #define MC34708_REVISION_FIN (0x07 << 6)
  32. #define MC34708_REVISION_FAB (0x07 << 9)
  33. #define MC13XXX_PWRCTRL 15
  34. #define MC13XXX_PWRCTRL_WDIRESET (1 << 12)
  35. #define MC13XXX_ADC1 44
  36. #define MC13XXX_ADC1_ADEN (1 << 0)
  37. #define MC13XXX_ADC1_RAND (1 << 1)
  38. #define MC13XXX_ADC1_ADSEL (1 << 3)
  39. #define MC13XXX_ADC1_ASC (1 << 20)
  40. #define MC13XXX_ADC1_ADTRIGIGN (1 << 21)
  41. #define MC13XXX_ADC2 45
  42. void mc13xxx_lock(struct mc13xxx *mc13xxx)
  43. {
  44. if (!mutex_trylock(&mc13xxx->lock)) {
  45. dev_dbg(mc13xxx->dev, "wait for %s from %ps\n",
  46. __func__, __builtin_return_address(0));
  47. mutex_lock(&mc13xxx->lock);
  48. }
  49. dev_dbg(mc13xxx->dev, "%s from %ps\n",
  50. __func__, __builtin_return_address(0));
  51. }
  52. EXPORT_SYMBOL(mc13xxx_lock);
  53. void mc13xxx_unlock(struct mc13xxx *mc13xxx)
  54. {
  55. dev_dbg(mc13xxx->dev, "%s from %ps\n",
  56. __func__, __builtin_return_address(0));
  57. mutex_unlock(&mc13xxx->lock);
  58. }
  59. EXPORT_SYMBOL(mc13xxx_unlock);
  60. int mc13xxx_reg_read(struct mc13xxx *mc13xxx, unsigned int offset, u32 *val)
  61. {
  62. int ret;
  63. ret = regmap_read(mc13xxx->regmap, offset, val);
  64. dev_vdbg(mc13xxx->dev, "[0x%02x] -> 0x%06x\n", offset, *val);
  65. return ret;
  66. }
  67. EXPORT_SYMBOL(mc13xxx_reg_read);
  68. int mc13xxx_reg_write(struct mc13xxx *mc13xxx, unsigned int offset, u32 val)
  69. {
  70. dev_vdbg(mc13xxx->dev, "[0x%02x] <- 0x%06x\n", offset, val);
  71. if (val >= BIT(24))
  72. return -EINVAL;
  73. return regmap_write(mc13xxx->regmap, offset, val);
  74. }
  75. EXPORT_SYMBOL(mc13xxx_reg_write);
  76. int mc13xxx_reg_rmw(struct mc13xxx *mc13xxx, unsigned int offset,
  77. u32 mask, u32 val)
  78. {
  79. BUG_ON(val & ~mask);
  80. dev_vdbg(mc13xxx->dev, "[0x%02x] <- 0x%06x (mask: 0x%06x)\n",
  81. offset, val, mask);
  82. return regmap_update_bits(mc13xxx->regmap, offset, mask, val);
  83. }
  84. EXPORT_SYMBOL(mc13xxx_reg_rmw);
  85. int mc13xxx_irq_mask(struct mc13xxx *mc13xxx, int irq)
  86. {
  87. int virq = regmap_irq_get_virq(mc13xxx->irq_data, irq);
  88. disable_irq_nosync(virq);
  89. return 0;
  90. }
  91. EXPORT_SYMBOL(mc13xxx_irq_mask);
  92. int mc13xxx_irq_unmask(struct mc13xxx *mc13xxx, int irq)
  93. {
  94. int virq = regmap_irq_get_virq(mc13xxx->irq_data, irq);
  95. enable_irq(virq);
  96. return 0;
  97. }
  98. EXPORT_SYMBOL(mc13xxx_irq_unmask);
  99. int mc13xxx_irq_status(struct mc13xxx *mc13xxx, int irq,
  100. int *enabled, int *pending)
  101. {
  102. int ret;
  103. unsigned int offmask = irq < 24 ? MC13XXX_IRQMASK0 : MC13XXX_IRQMASK1;
  104. unsigned int offstat = irq < 24 ? MC13XXX_IRQSTAT0 : MC13XXX_IRQSTAT1;
  105. u32 irqbit = 1 << (irq < 24 ? irq : irq - 24);
  106. if (irq < 0 || irq >= ARRAY_SIZE(mc13xxx->irqs))
  107. return -EINVAL;
  108. if (enabled) {
  109. u32 mask;
  110. ret = mc13xxx_reg_read(mc13xxx, offmask, &mask);
  111. if (ret)
  112. return ret;
  113. *enabled = mask & irqbit;
  114. }
  115. if (pending) {
  116. u32 stat;
  117. ret = mc13xxx_reg_read(mc13xxx, offstat, &stat);
  118. if (ret)
  119. return ret;
  120. *pending = stat & irqbit;
  121. }
  122. return 0;
  123. }
  124. EXPORT_SYMBOL(mc13xxx_irq_status);
  125. int mc13xxx_irq_request(struct mc13xxx *mc13xxx, int irq,
  126. irq_handler_t handler, const char *name, void *dev)
  127. {
  128. int virq = regmap_irq_get_virq(mc13xxx->irq_data, irq);
  129. return devm_request_threaded_irq(mc13xxx->dev, virq, NULL, handler,
  130. IRQF_ONESHOT, name, dev);
  131. }
  132. EXPORT_SYMBOL(mc13xxx_irq_request);
  133. int mc13xxx_irq_free(struct mc13xxx *mc13xxx, int irq, void *dev)
  134. {
  135. int virq = regmap_irq_get_virq(mc13xxx->irq_data, irq);
  136. devm_free_irq(mc13xxx->dev, virq, dev);
  137. return 0;
  138. }
  139. EXPORT_SYMBOL(mc13xxx_irq_free);
  140. #define maskval(reg, mask) (((reg) & (mask)) >> __ffs(mask))
  141. static void mc13xxx_print_revision(struct mc13xxx *mc13xxx, u32 revision)
  142. {
  143. dev_info(mc13xxx->dev, "%s: rev: %d.%d, "
  144. "fin: %d, fab: %d, icid: %d/%d\n",
  145. mc13xxx->variant->name,
  146. maskval(revision, MC13XXX_REVISION_REVFULL),
  147. maskval(revision, MC13XXX_REVISION_REVMETAL),
  148. maskval(revision, MC13XXX_REVISION_FIN),
  149. maskval(revision, MC13XXX_REVISION_FAB),
  150. maskval(revision, MC13XXX_REVISION_ICID),
  151. maskval(revision, MC13XXX_REVISION_ICIDCODE));
  152. }
  153. static void mc34708_print_revision(struct mc13xxx *mc13xxx, u32 revision)
  154. {
  155. dev_info(mc13xxx->dev, "%s: rev %d.%d, fin: %d, fab: %d\n",
  156. mc13xxx->variant->name,
  157. maskval(revision, MC34708_REVISION_REVFULL),
  158. maskval(revision, MC34708_REVISION_REVMETAL),
  159. maskval(revision, MC34708_REVISION_FIN),
  160. maskval(revision, MC34708_REVISION_FAB));
  161. }
  162. /* These are only exported for mc13xxx-i2c and mc13xxx-spi */
  163. struct mc13xxx_variant mc13xxx_variant_mc13783 = {
  164. .name = "mc13783",
  165. .print_revision = mc13xxx_print_revision,
  166. };
  167. EXPORT_SYMBOL_GPL(mc13xxx_variant_mc13783);
  168. struct mc13xxx_variant mc13xxx_variant_mc13892 = {
  169. .name = "mc13892",
  170. .print_revision = mc13xxx_print_revision,
  171. };
  172. EXPORT_SYMBOL_GPL(mc13xxx_variant_mc13892);
  173. struct mc13xxx_variant mc13xxx_variant_mc34708 = {
  174. .name = "mc34708",
  175. .print_revision = mc34708_print_revision,
  176. };
  177. EXPORT_SYMBOL_GPL(mc13xxx_variant_mc34708);
  178. static const char *mc13xxx_get_chipname(struct mc13xxx *mc13xxx)
  179. {
  180. return mc13xxx->variant->name;
  181. }
  182. int mc13xxx_get_flags(struct mc13xxx *mc13xxx)
  183. {
  184. return mc13xxx->flags;
  185. }
  186. EXPORT_SYMBOL(mc13xxx_get_flags);
  187. #define MC13XXX_ADC1_CHAN0_SHIFT 5
  188. #define MC13XXX_ADC1_CHAN1_SHIFT 8
  189. #define MC13783_ADC1_ATO_SHIFT 11
  190. #define MC13783_ADC1_ATOX (1 << 19)
  191. struct mc13xxx_adcdone_data {
  192. struct mc13xxx *mc13xxx;
  193. struct completion done;
  194. };
  195. static irqreturn_t mc13xxx_handler_adcdone(int irq, void *data)
  196. {
  197. struct mc13xxx_adcdone_data *adcdone_data = data;
  198. complete_all(&adcdone_data->done);
  199. return IRQ_HANDLED;
  200. }
  201. #define MC13XXX_ADC_WORKING (1 << 0)
  202. int mc13xxx_adc_do_conversion(struct mc13xxx *mc13xxx, unsigned int mode,
  203. unsigned int channel, u8 ato, bool atox,
  204. unsigned int *sample)
  205. {
  206. u32 adc0, adc1, old_adc0;
  207. int i, ret;
  208. struct mc13xxx_adcdone_data adcdone_data = {
  209. .mc13xxx = mc13xxx,
  210. };
  211. init_completion(&adcdone_data.done);
  212. dev_dbg(mc13xxx->dev, "%s\n", __func__);
  213. mc13xxx_lock(mc13xxx);
  214. if (mc13xxx->adcflags & MC13XXX_ADC_WORKING) {
  215. ret = -EBUSY;
  216. goto out;
  217. }
  218. mc13xxx->adcflags |= MC13XXX_ADC_WORKING;
  219. mc13xxx_reg_read(mc13xxx, MC13XXX_ADC0, &old_adc0);
  220. adc0 = MC13XXX_ADC0_ADINC1 | MC13XXX_ADC0_ADINC2;
  221. adc1 = MC13XXX_ADC1_ADEN | MC13XXX_ADC1_ADTRIGIGN | MC13XXX_ADC1_ASC;
  222. if (channel > 7)
  223. adc1 |= MC13XXX_ADC1_ADSEL;
  224. switch (mode) {
  225. case MC13XXX_ADC_MODE_TS:
  226. adc0 |= MC13XXX_ADC0_ADREFEN | MC13XXX_ADC0_TSMOD0 |
  227. MC13XXX_ADC0_TSMOD1;
  228. adc1 |= 4 << MC13XXX_ADC1_CHAN1_SHIFT;
  229. break;
  230. case MC13XXX_ADC_MODE_SINGLE_CHAN:
  231. adc0 |= old_adc0 & MC13XXX_ADC0_CONFIG_MASK;
  232. adc1 |= (channel & 0x7) << MC13XXX_ADC1_CHAN0_SHIFT;
  233. adc1 |= MC13XXX_ADC1_RAND;
  234. break;
  235. case MC13XXX_ADC_MODE_MULT_CHAN:
  236. adc0 |= old_adc0 & MC13XXX_ADC0_CONFIG_MASK;
  237. adc1 |= 4 << MC13XXX_ADC1_CHAN1_SHIFT;
  238. break;
  239. default:
  240. mc13xxx_unlock(mc13xxx);
  241. return -EINVAL;
  242. }
  243. adc1 |= ato << MC13783_ADC1_ATO_SHIFT;
  244. if (atox)
  245. adc1 |= MC13783_ADC1_ATOX;
  246. dev_dbg(mc13xxx->dev, "%s: request irq\n", __func__);
  247. mc13xxx_irq_request(mc13xxx, MC13XXX_IRQ_ADCDONE,
  248. mc13xxx_handler_adcdone, __func__, &adcdone_data);
  249. mc13xxx_reg_write(mc13xxx, MC13XXX_ADC0, adc0);
  250. mc13xxx_reg_write(mc13xxx, MC13XXX_ADC1, adc1);
  251. mc13xxx_unlock(mc13xxx);
  252. ret = wait_for_completion_interruptible_timeout(&adcdone_data.done, HZ);
  253. if (!ret)
  254. ret = -ETIMEDOUT;
  255. mc13xxx_lock(mc13xxx);
  256. mc13xxx_irq_free(mc13xxx, MC13XXX_IRQ_ADCDONE, &adcdone_data);
  257. if (ret > 0)
  258. for (i = 0; i < 4; ++i) {
  259. ret = mc13xxx_reg_read(mc13xxx,
  260. MC13XXX_ADC2, &sample[i]);
  261. if (ret)
  262. break;
  263. }
  264. if (mode == MC13XXX_ADC_MODE_TS)
  265. /* restore TSMOD */
  266. mc13xxx_reg_write(mc13xxx, MC13XXX_ADC0, old_adc0);
  267. mc13xxx->adcflags &= ~MC13XXX_ADC_WORKING;
  268. out:
  269. mc13xxx_unlock(mc13xxx);
  270. return ret;
  271. }
  272. EXPORT_SYMBOL_GPL(mc13xxx_adc_do_conversion);
  273. static int mc13xxx_add_subdevice_pdata(struct mc13xxx *mc13xxx,
  274. const char *format, void *pdata, size_t pdata_size)
  275. {
  276. char buf[30];
  277. const char *name = mc13xxx_get_chipname(mc13xxx);
  278. struct mfd_cell cell = {
  279. .platform_data = pdata,
  280. .pdata_size = pdata_size,
  281. };
  282. /* there is no asnprintf in the kernel :-( */
  283. if (snprintf(buf, sizeof(buf), format, name) > sizeof(buf))
  284. return -E2BIG;
  285. cell.name = kmemdup(buf, strlen(buf) + 1, GFP_KERNEL);
  286. if (!cell.name)
  287. return -ENOMEM;
  288. return mfd_add_devices(mc13xxx->dev, -1, &cell, 1, NULL, 0,
  289. regmap_irq_get_domain(mc13xxx->irq_data));
  290. }
  291. static int mc13xxx_add_subdevice(struct mc13xxx *mc13xxx, const char *format)
  292. {
  293. return mc13xxx_add_subdevice_pdata(mc13xxx, format, NULL, 0);
  294. }
  295. #ifdef CONFIG_OF
  296. static int mc13xxx_probe_flags_dt(struct mc13xxx *mc13xxx)
  297. {
  298. struct device_node *np = mc13xxx->dev->of_node;
  299. if (!np)
  300. return -ENODEV;
  301. if (of_property_read_bool(np, "fsl,mc13xxx-uses-adc"))
  302. mc13xxx->flags |= MC13XXX_USE_ADC;
  303. if (of_property_read_bool(np, "fsl,mc13xxx-uses-codec"))
  304. mc13xxx->flags |= MC13XXX_USE_CODEC;
  305. if (of_property_read_bool(np, "fsl,mc13xxx-uses-rtc"))
  306. mc13xxx->flags |= MC13XXX_USE_RTC;
  307. if (of_property_read_bool(np, "fsl,mc13xxx-uses-touch"))
  308. mc13xxx->flags |= MC13XXX_USE_TOUCHSCREEN;
  309. return 0;
  310. }
  311. #else
  312. static inline int mc13xxx_probe_flags_dt(struct mc13xxx *mc13xxx)
  313. {
  314. return -ENODEV;
  315. }
  316. #endif
  317. int mc13xxx_common_init(struct device *dev)
  318. {
  319. struct mc13xxx_platform_data *pdata = dev_get_platdata(dev);
  320. struct mc13xxx *mc13xxx = dev_get_drvdata(dev);
  321. u32 revision;
  322. int i, ret;
  323. mc13xxx->dev = dev;
  324. ret = mc13xxx_reg_read(mc13xxx, MC13XXX_REVISION, &revision);
  325. if (ret)
  326. return ret;
  327. mc13xxx->variant->print_revision(mc13xxx, revision);
  328. ret = mc13xxx_reg_rmw(mc13xxx, MC13XXX_PWRCTRL,
  329. MC13XXX_PWRCTRL_WDIRESET, MC13XXX_PWRCTRL_WDIRESET);
  330. if (ret)
  331. return ret;
  332. for (i = 0; i < ARRAY_SIZE(mc13xxx->irqs); i++) {
  333. mc13xxx->irqs[i].reg_offset = i / MC13XXX_IRQ_PER_REG;
  334. mc13xxx->irqs[i].mask = BIT(i % MC13XXX_IRQ_PER_REG);
  335. }
  336. mc13xxx->irq_chip.name = dev_name(dev);
  337. mc13xxx->irq_chip.status_base = MC13XXX_IRQSTAT0;
  338. mc13xxx->irq_chip.mask_base = MC13XXX_IRQMASK0;
  339. mc13xxx->irq_chip.ack_base = MC13XXX_IRQSTAT0;
  340. mc13xxx->irq_chip.irq_reg_stride = MC13XXX_IRQSTAT1 - MC13XXX_IRQSTAT0;
  341. mc13xxx->irq_chip.init_ack_masked = true;
  342. mc13xxx->irq_chip.use_ack = true;
  343. mc13xxx->irq_chip.num_regs = MC13XXX_IRQ_REG_CNT;
  344. mc13xxx->irq_chip.irqs = mc13xxx->irqs;
  345. mc13xxx->irq_chip.num_irqs = ARRAY_SIZE(mc13xxx->irqs);
  346. ret = regmap_add_irq_chip(mc13xxx->regmap, mc13xxx->irq, IRQF_ONESHOT,
  347. 0, &mc13xxx->irq_chip, &mc13xxx->irq_data);
  348. if (ret)
  349. return ret;
  350. mutex_init(&mc13xxx->lock);
  351. if (mc13xxx_probe_flags_dt(mc13xxx) < 0 && pdata)
  352. mc13xxx->flags = pdata->flags;
  353. if (pdata) {
  354. mc13xxx_add_subdevice_pdata(mc13xxx, "%s-regulator",
  355. &pdata->regulators, sizeof(pdata->regulators));
  356. mc13xxx_add_subdevice_pdata(mc13xxx, "%s-led",
  357. pdata->leds, sizeof(*pdata->leds));
  358. mc13xxx_add_subdevice_pdata(mc13xxx, "%s-pwrbutton",
  359. pdata->buttons, sizeof(*pdata->buttons));
  360. if (mc13xxx->flags & MC13XXX_USE_CODEC)
  361. mc13xxx_add_subdevice_pdata(mc13xxx, "%s-codec",
  362. pdata->codec, sizeof(*pdata->codec));
  363. if (mc13xxx->flags & MC13XXX_USE_TOUCHSCREEN)
  364. mc13xxx_add_subdevice_pdata(mc13xxx, "%s-ts",
  365. &pdata->touch, sizeof(pdata->touch));
  366. } else {
  367. mc13xxx_add_subdevice(mc13xxx, "%s-regulator");
  368. mc13xxx_add_subdevice(mc13xxx, "%s-led");
  369. mc13xxx_add_subdevice(mc13xxx, "%s-pwrbutton");
  370. if (mc13xxx->flags & MC13XXX_USE_CODEC)
  371. mc13xxx_add_subdevice(mc13xxx, "%s-codec");
  372. if (mc13xxx->flags & MC13XXX_USE_TOUCHSCREEN)
  373. mc13xxx_add_subdevice(mc13xxx, "%s-ts");
  374. }
  375. if (mc13xxx->flags & MC13XXX_USE_ADC)
  376. mc13xxx_add_subdevice(mc13xxx, "%s-adc");
  377. if (mc13xxx->flags & MC13XXX_USE_RTC)
  378. mc13xxx_add_subdevice(mc13xxx, "%s-rtc");
  379. return 0;
  380. }
  381. EXPORT_SYMBOL_GPL(mc13xxx_common_init);
  382. int mc13xxx_common_exit(struct device *dev)
  383. {
  384. struct mc13xxx *mc13xxx = dev_get_drvdata(dev);
  385. mfd_remove_devices(dev);
  386. regmap_del_irq_chip(mc13xxx->irq, mc13xxx->irq_data);
  387. mutex_destroy(&mc13xxx->lock);
  388. return 0;
  389. }
  390. EXPORT_SYMBOL_GPL(mc13xxx_common_exit);
  391. MODULE_DESCRIPTION("Core driver for Freescale MC13XXX PMIC");
  392. MODULE_AUTHOR("Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>");
  393. MODULE_LICENSE("GPL v2");