wm8350-core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * wm8350-core.c -- Device access for Wolfson WM8350
  3. *
  4. * Copyright 2007, 2008 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Liam Girdwood, Mark Brown
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/bug.h>
  18. #include <linux/device.h>
  19. #include <linux/delay.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/regmap.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/mfd/wm8350/core.h>
  24. #include <linux/mfd/wm8350/audio.h>
  25. #include <linux/mfd/wm8350/comparator.h>
  26. #include <linux/mfd/wm8350/gpio.h>
  27. #include <linux/mfd/wm8350/pmic.h>
  28. #include <linux/mfd/wm8350/rtc.h>
  29. #include <linux/mfd/wm8350/supply.h>
  30. #include <linux/mfd/wm8350/wdt.h>
  31. #define WM8350_CLOCK_CONTROL_1 0x28
  32. #define WM8350_AIF_TEST 0x74
  33. /* debug */
  34. #define WM8350_BUS_DEBUG 0
  35. #if WM8350_BUS_DEBUG
  36. #define dump(regs, src) do { \
  37. int i_; \
  38. u16 *src_ = src; \
  39. printk(KERN_DEBUG); \
  40. for (i_ = 0; i_ < regs; i_++) \
  41. printk(" 0x%4.4x", *src_++); \
  42. printk("\n"); \
  43. } while (0);
  44. #else
  45. #define dump(bytes, src)
  46. #endif
  47. #define WM8350_LOCK_DEBUG 0
  48. #if WM8350_LOCK_DEBUG
  49. #define ldbg(format, arg...) printk(format, ## arg)
  50. #else
  51. #define ldbg(format, arg...)
  52. #endif
  53. /*
  54. * WM8350 Device IO
  55. */
  56. static DEFINE_MUTEX(reg_lock_mutex);
  57. /*
  58. * Safe read, modify, write methods
  59. */
  60. int wm8350_clear_bits(struct wm8350 *wm8350, u16 reg, u16 mask)
  61. {
  62. return regmap_update_bits(wm8350->regmap, reg, mask, 0);
  63. }
  64. EXPORT_SYMBOL_GPL(wm8350_clear_bits);
  65. int wm8350_set_bits(struct wm8350 *wm8350, u16 reg, u16 mask)
  66. {
  67. return regmap_update_bits(wm8350->regmap, reg, mask, mask);
  68. }
  69. EXPORT_SYMBOL_GPL(wm8350_set_bits);
  70. u16 wm8350_reg_read(struct wm8350 *wm8350, int reg)
  71. {
  72. unsigned int data;
  73. int err;
  74. err = regmap_read(wm8350->regmap, reg, &data);
  75. if (err)
  76. dev_err(wm8350->dev, "read from reg R%d failed\n", reg);
  77. return data;
  78. }
  79. EXPORT_SYMBOL_GPL(wm8350_reg_read);
  80. int wm8350_reg_write(struct wm8350 *wm8350, int reg, u16 val)
  81. {
  82. int ret;
  83. ret = regmap_write(wm8350->regmap, reg, val);
  84. if (ret)
  85. dev_err(wm8350->dev, "write to reg R%d failed\n", reg);
  86. return ret;
  87. }
  88. EXPORT_SYMBOL_GPL(wm8350_reg_write);
  89. int wm8350_block_read(struct wm8350 *wm8350, int start_reg, int regs,
  90. u16 *dest)
  91. {
  92. int err = 0;
  93. err = regmap_bulk_read(wm8350->regmap, start_reg, dest, regs);
  94. if (err)
  95. dev_err(wm8350->dev, "block read starting from R%d failed\n",
  96. start_reg);
  97. return err;
  98. }
  99. EXPORT_SYMBOL_GPL(wm8350_block_read);
  100. int wm8350_block_write(struct wm8350 *wm8350, int start_reg, int regs,
  101. u16 *src)
  102. {
  103. int ret = 0;
  104. ret = regmap_bulk_write(wm8350->regmap, start_reg, src, regs);
  105. if (ret)
  106. dev_err(wm8350->dev, "block write starting at R%d failed\n",
  107. start_reg);
  108. return ret;
  109. }
  110. EXPORT_SYMBOL_GPL(wm8350_block_write);
  111. /**
  112. * wm8350_reg_lock()
  113. *
  114. * The WM8350 has a hardware lock which can be used to prevent writes to
  115. * some registers (generally those which can cause particularly serious
  116. * problems if misused). This function enables that lock.
  117. */
  118. int wm8350_reg_lock(struct wm8350 *wm8350)
  119. {
  120. int ret;
  121. mutex_lock(&reg_lock_mutex);
  122. ldbg(__func__);
  123. ret = wm8350_reg_write(wm8350, WM8350_SECURITY, WM8350_LOCK_KEY);
  124. if (ret)
  125. dev_err(wm8350->dev, "lock failed\n");
  126. wm8350->unlocked = false;
  127. mutex_unlock(&reg_lock_mutex);
  128. return ret;
  129. }
  130. EXPORT_SYMBOL_GPL(wm8350_reg_lock);
  131. /**
  132. * wm8350_reg_unlock()
  133. *
  134. * The WM8350 has a hardware lock which can be used to prevent writes to
  135. * some registers (generally those which can cause particularly serious
  136. * problems if misused). This function disables that lock so updates
  137. * can be performed. For maximum safety this should be done only when
  138. * required.
  139. */
  140. int wm8350_reg_unlock(struct wm8350 *wm8350)
  141. {
  142. int ret;
  143. mutex_lock(&reg_lock_mutex);
  144. ldbg(__func__);
  145. ret = wm8350_reg_write(wm8350, WM8350_SECURITY, WM8350_UNLOCK_KEY);
  146. if (ret)
  147. dev_err(wm8350->dev, "unlock failed\n");
  148. wm8350->unlocked = true;
  149. mutex_unlock(&reg_lock_mutex);
  150. return ret;
  151. }
  152. EXPORT_SYMBOL_GPL(wm8350_reg_unlock);
  153. int wm8350_read_auxadc(struct wm8350 *wm8350, int channel, int scale, int vref)
  154. {
  155. u16 reg, result = 0;
  156. if (channel < WM8350_AUXADC_AUX1 || channel > WM8350_AUXADC_TEMP)
  157. return -EINVAL;
  158. if (channel >= WM8350_AUXADC_USB && channel <= WM8350_AUXADC_TEMP
  159. && (scale != 0 || vref != 0))
  160. return -EINVAL;
  161. mutex_lock(&wm8350->auxadc_mutex);
  162. /* Turn on the ADC */
  163. reg = wm8350_reg_read(wm8350, WM8350_POWER_MGMT_5);
  164. wm8350_reg_write(wm8350, WM8350_POWER_MGMT_5, reg | WM8350_AUXADC_ENA);
  165. if (scale || vref) {
  166. reg = scale << 13;
  167. reg |= vref << 12;
  168. wm8350_reg_write(wm8350, WM8350_AUX1_READBACK + channel, reg);
  169. }
  170. reg = wm8350_reg_read(wm8350, WM8350_DIGITISER_CONTROL_1);
  171. reg |= 1 << channel | WM8350_AUXADC_POLL;
  172. wm8350_reg_write(wm8350, WM8350_DIGITISER_CONTROL_1, reg);
  173. /* If a late IRQ left the completion signalled then consume
  174. * the completion. */
  175. try_wait_for_completion(&wm8350->auxadc_done);
  176. /* We ignore the result of the completion and just check for a
  177. * conversion result, allowing us to soldier on if the IRQ
  178. * infrastructure is not set up for the chip. */
  179. wait_for_completion_timeout(&wm8350->auxadc_done, msecs_to_jiffies(5));
  180. reg = wm8350_reg_read(wm8350, WM8350_DIGITISER_CONTROL_1);
  181. if (reg & WM8350_AUXADC_POLL)
  182. dev_err(wm8350->dev, "adc chn %d read timeout\n", channel);
  183. else
  184. result = wm8350_reg_read(wm8350,
  185. WM8350_AUX1_READBACK + channel);
  186. /* Turn off the ADC */
  187. reg = wm8350_reg_read(wm8350, WM8350_POWER_MGMT_5);
  188. wm8350_reg_write(wm8350, WM8350_POWER_MGMT_5,
  189. reg & ~WM8350_AUXADC_ENA);
  190. mutex_unlock(&wm8350->auxadc_mutex);
  191. return result & WM8350_AUXADC_DATA1_MASK;
  192. }
  193. EXPORT_SYMBOL_GPL(wm8350_read_auxadc);
  194. static irqreturn_t wm8350_auxadc_irq(int irq, void *irq_data)
  195. {
  196. struct wm8350 *wm8350 = irq_data;
  197. complete(&wm8350->auxadc_done);
  198. return IRQ_HANDLED;
  199. }
  200. /*
  201. * Register a client device. This is non-fatal since there is no need to
  202. * fail the entire device init due to a single platform device failing.
  203. */
  204. static void wm8350_client_dev_register(struct wm8350 *wm8350,
  205. const char *name,
  206. struct platform_device **pdev)
  207. {
  208. int ret;
  209. *pdev = platform_device_alloc(name, -1);
  210. if (*pdev == NULL) {
  211. dev_err(wm8350->dev, "Failed to allocate %s\n", name);
  212. return;
  213. }
  214. (*pdev)->dev.parent = wm8350->dev;
  215. platform_set_drvdata(*pdev, wm8350);
  216. ret = platform_device_add(*pdev);
  217. if (ret != 0) {
  218. dev_err(wm8350->dev, "Failed to register %s: %d\n", name, ret);
  219. platform_device_put(*pdev);
  220. *pdev = NULL;
  221. }
  222. }
  223. int wm8350_device_init(struct wm8350 *wm8350, int irq,
  224. struct wm8350_platform_data *pdata)
  225. {
  226. int ret;
  227. unsigned int id1, id2, mask_rev;
  228. unsigned int cust_id, mode, chip_rev;
  229. dev_set_drvdata(wm8350->dev, wm8350);
  230. /* get WM8350 revision and config mode */
  231. ret = regmap_read(wm8350->regmap, WM8350_RESET_ID, &id1);
  232. if (ret != 0) {
  233. dev_err(wm8350->dev, "Failed to read ID: %d\n", ret);
  234. goto err;
  235. }
  236. ret = regmap_read(wm8350->regmap, WM8350_ID, &id2);
  237. if (ret != 0) {
  238. dev_err(wm8350->dev, "Failed to read ID: %d\n", ret);
  239. goto err;
  240. }
  241. ret = regmap_read(wm8350->regmap, WM8350_REVISION, &mask_rev);
  242. if (ret != 0) {
  243. dev_err(wm8350->dev, "Failed to read revision: %d\n", ret);
  244. goto err;
  245. }
  246. if (id1 != 0x6143) {
  247. dev_err(wm8350->dev,
  248. "Device with ID %x is not a WM8350\n", id1);
  249. ret = -ENODEV;
  250. goto err;
  251. }
  252. mode = (id2 & WM8350_CONF_STS_MASK) >> 10;
  253. cust_id = id2 & WM8350_CUST_ID_MASK;
  254. chip_rev = (id2 & WM8350_CHIP_REV_MASK) >> 12;
  255. dev_info(wm8350->dev,
  256. "CONF_STS %d, CUST_ID %d, MASK_REV %d, CHIP_REV %d\n",
  257. mode, cust_id, mask_rev, chip_rev);
  258. if (cust_id != 0) {
  259. dev_err(wm8350->dev, "Unsupported CUST_ID\n");
  260. ret = -ENODEV;
  261. goto err;
  262. }
  263. switch (mask_rev) {
  264. case 0:
  265. wm8350->pmic.max_dcdc = WM8350_DCDC_6;
  266. wm8350->pmic.max_isink = WM8350_ISINK_B;
  267. switch (chip_rev) {
  268. case WM8350_REV_E:
  269. dev_info(wm8350->dev, "WM8350 Rev E\n");
  270. break;
  271. case WM8350_REV_F:
  272. dev_info(wm8350->dev, "WM8350 Rev F\n");
  273. break;
  274. case WM8350_REV_G:
  275. dev_info(wm8350->dev, "WM8350 Rev G\n");
  276. wm8350->power.rev_g_coeff = 1;
  277. break;
  278. case WM8350_REV_H:
  279. dev_info(wm8350->dev, "WM8350 Rev H\n");
  280. wm8350->power.rev_g_coeff = 1;
  281. break;
  282. default:
  283. /* For safety we refuse to run on unknown hardware */
  284. dev_err(wm8350->dev, "Unknown WM8350 CHIP_REV\n");
  285. ret = -ENODEV;
  286. goto err;
  287. }
  288. break;
  289. case 1:
  290. wm8350->pmic.max_dcdc = WM8350_DCDC_4;
  291. wm8350->pmic.max_isink = WM8350_ISINK_A;
  292. switch (chip_rev) {
  293. case 0:
  294. dev_info(wm8350->dev, "WM8351 Rev A\n");
  295. wm8350->power.rev_g_coeff = 1;
  296. break;
  297. case 1:
  298. dev_info(wm8350->dev, "WM8351 Rev B\n");
  299. wm8350->power.rev_g_coeff = 1;
  300. break;
  301. default:
  302. dev_err(wm8350->dev, "Unknown WM8351 CHIP_REV\n");
  303. ret = -ENODEV;
  304. goto err;
  305. }
  306. break;
  307. case 2:
  308. wm8350->pmic.max_dcdc = WM8350_DCDC_6;
  309. wm8350->pmic.max_isink = WM8350_ISINK_B;
  310. switch (chip_rev) {
  311. case 0:
  312. dev_info(wm8350->dev, "WM8352 Rev A\n");
  313. wm8350->power.rev_g_coeff = 1;
  314. break;
  315. default:
  316. dev_err(wm8350->dev, "Unknown WM8352 CHIP_REV\n");
  317. ret = -ENODEV;
  318. goto err;
  319. }
  320. break;
  321. default:
  322. dev_err(wm8350->dev, "Unknown MASK_REV\n");
  323. ret = -ENODEV;
  324. goto err;
  325. }
  326. mutex_init(&wm8350->auxadc_mutex);
  327. init_completion(&wm8350->auxadc_done);
  328. ret = wm8350_irq_init(wm8350, irq, pdata);
  329. if (ret < 0)
  330. goto err;
  331. if (wm8350->irq_base) {
  332. ret = request_threaded_irq(wm8350->irq_base +
  333. WM8350_IRQ_AUXADC_DATARDY,
  334. NULL, wm8350_auxadc_irq,
  335. IRQF_ONESHOT,
  336. "auxadc", wm8350);
  337. if (ret < 0)
  338. dev_warn(wm8350->dev,
  339. "Failed to request AUXADC IRQ: %d\n", ret);
  340. }
  341. if (pdata && pdata->init) {
  342. ret = pdata->init(wm8350);
  343. if (ret != 0) {
  344. dev_err(wm8350->dev, "Platform init() failed: %d\n",
  345. ret);
  346. goto err_irq;
  347. }
  348. }
  349. wm8350_reg_write(wm8350, WM8350_SYSTEM_INTERRUPTS_MASK, 0x0);
  350. wm8350_client_dev_register(wm8350, "wm8350-codec",
  351. &(wm8350->codec.pdev));
  352. wm8350_client_dev_register(wm8350, "wm8350-gpio",
  353. &(wm8350->gpio.pdev));
  354. wm8350_client_dev_register(wm8350, "wm8350-hwmon",
  355. &(wm8350->hwmon.pdev));
  356. wm8350_client_dev_register(wm8350, "wm8350-power",
  357. &(wm8350->power.pdev));
  358. wm8350_client_dev_register(wm8350, "wm8350-rtc", &(wm8350->rtc.pdev));
  359. wm8350_client_dev_register(wm8350, "wm8350-wdt", &(wm8350->wdt.pdev));
  360. return 0;
  361. err_irq:
  362. wm8350_irq_exit(wm8350);
  363. err:
  364. return ret;
  365. }
  366. EXPORT_SYMBOL_GPL(wm8350_device_init);
  367. void wm8350_device_exit(struct wm8350 *wm8350)
  368. {
  369. int i;
  370. for (i = 0; i < ARRAY_SIZE(wm8350->pmic.led); i++)
  371. platform_device_unregister(wm8350->pmic.led[i].pdev);
  372. for (i = 0; i < ARRAY_SIZE(wm8350->pmic.pdev); i++)
  373. platform_device_unregister(wm8350->pmic.pdev[i]);
  374. platform_device_unregister(wm8350->wdt.pdev);
  375. platform_device_unregister(wm8350->rtc.pdev);
  376. platform_device_unregister(wm8350->power.pdev);
  377. platform_device_unregister(wm8350->hwmon.pdev);
  378. platform_device_unregister(wm8350->gpio.pdev);
  379. platform_device_unregister(wm8350->codec.pdev);
  380. if (wm8350->irq_base)
  381. free_irq(wm8350->irq_base + WM8350_IRQ_AUXADC_DATARDY, wm8350);
  382. wm8350_irq_exit(wm8350);
  383. }
  384. EXPORT_SYMBOL_GPL(wm8350_device_exit);
  385. MODULE_DESCRIPTION("WM8350 AudioPlus PMIC core driver");
  386. MODULE_LICENSE("GPL");