wm8994-core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /*
  2. * wm8994-core.c -- Device access for Wolfson WM8994
  3. *
  4. * Copyright 2009 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  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/i2c.h>
  18. #include <linux/delay.h>
  19. #include <linux/mfd/core.h>
  20. #include <linux/regulator/consumer.h>
  21. #include <linux/regulator/machine.h>
  22. #include <linux/mfd/wm8994/core.h>
  23. #include <linux/mfd/wm8994/pdata.h>
  24. #include <linux/mfd/wm8994/registers.h>
  25. static int wm8994_read(struct wm8994 *wm8994, unsigned short reg,
  26. int bytes, void *dest)
  27. {
  28. int ret, i;
  29. u16 *buf = dest;
  30. BUG_ON(bytes % 2);
  31. BUG_ON(bytes <= 0);
  32. ret = wm8994->read_dev(wm8994, reg, bytes, dest);
  33. if (ret < 0)
  34. return ret;
  35. for (i = 0; i < bytes / 2; i++) {
  36. buf[i] = be16_to_cpu(buf[i]);
  37. dev_vdbg(wm8994->dev, "Read %04x from R%d(0x%x)\n",
  38. buf[i], reg + i, reg + i);
  39. }
  40. return 0;
  41. }
  42. /**
  43. * wm8994_reg_read: Read a single WM8994 register.
  44. *
  45. * @wm8994: Device to read from.
  46. * @reg: Register to read.
  47. */
  48. int wm8994_reg_read(struct wm8994 *wm8994, unsigned short reg)
  49. {
  50. unsigned short val;
  51. int ret;
  52. mutex_lock(&wm8994->io_lock);
  53. ret = wm8994_read(wm8994, reg, 2, &val);
  54. mutex_unlock(&wm8994->io_lock);
  55. if (ret < 0)
  56. return ret;
  57. else
  58. return val;
  59. }
  60. EXPORT_SYMBOL_GPL(wm8994_reg_read);
  61. /**
  62. * wm8994_bulk_read: Read multiple WM8994 registers
  63. *
  64. * @wm8994: Device to read from
  65. * @reg: First register
  66. * @count: Number of registers
  67. * @buf: Buffer to fill.
  68. */
  69. int wm8994_bulk_read(struct wm8994 *wm8994, unsigned short reg,
  70. int count, u16 *buf)
  71. {
  72. int ret;
  73. mutex_lock(&wm8994->io_lock);
  74. ret = wm8994_read(wm8994, reg, count * 2, buf);
  75. mutex_unlock(&wm8994->io_lock);
  76. return ret;
  77. }
  78. EXPORT_SYMBOL_GPL(wm8994_bulk_read);
  79. static int wm8994_write(struct wm8994 *wm8994, unsigned short reg,
  80. int bytes, void *src)
  81. {
  82. u16 *buf = src;
  83. int i;
  84. BUG_ON(bytes % 2);
  85. BUG_ON(bytes <= 0);
  86. for (i = 0; i < bytes / 2; i++) {
  87. dev_vdbg(wm8994->dev, "Write %04x to R%d(0x%x)\n",
  88. buf[i], reg + i, reg + i);
  89. buf[i] = cpu_to_be16(buf[i]);
  90. }
  91. return wm8994->write_dev(wm8994, reg, bytes, src);
  92. }
  93. /**
  94. * wm8994_reg_write: Write a single WM8994 register.
  95. *
  96. * @wm8994: Device to write to.
  97. * @reg: Register to write to.
  98. * @val: Value to write.
  99. */
  100. int wm8994_reg_write(struct wm8994 *wm8994, unsigned short reg,
  101. unsigned short val)
  102. {
  103. int ret;
  104. mutex_lock(&wm8994->io_lock);
  105. ret = wm8994_write(wm8994, reg, 2, &val);
  106. mutex_unlock(&wm8994->io_lock);
  107. return ret;
  108. }
  109. EXPORT_SYMBOL_GPL(wm8994_reg_write);
  110. /**
  111. * wm8994_set_bits: Set the value of a bitfield in a WM8994 register
  112. *
  113. * @wm8994: Device to write to.
  114. * @reg: Register to write to.
  115. * @mask: Mask of bits to set.
  116. * @val: Value to set (unshifted)
  117. */
  118. int wm8994_set_bits(struct wm8994 *wm8994, unsigned short reg,
  119. unsigned short mask, unsigned short val)
  120. {
  121. int ret;
  122. u16 r;
  123. mutex_lock(&wm8994->io_lock);
  124. ret = wm8994_read(wm8994, reg, 2, &r);
  125. if (ret < 0)
  126. goto out;
  127. r &= ~mask;
  128. r |= val;
  129. ret = wm8994_write(wm8994, reg, 2, &r);
  130. out:
  131. mutex_unlock(&wm8994->io_lock);
  132. return ret;
  133. }
  134. EXPORT_SYMBOL_GPL(wm8994_set_bits);
  135. static struct mfd_cell wm8994_regulator_devs[] = {
  136. { .name = "wm8994-ldo", .id = 1 },
  137. { .name = "wm8994-ldo", .id = 2 },
  138. };
  139. static struct mfd_cell wm8994_devs[] = {
  140. { .name = "wm8994-codec" },
  141. { .name = "wm8994-gpio" },
  142. };
  143. /*
  144. * Supplies for the main bulk of CODEC; the LDO supplies are ignored
  145. * and should be handled via the standard regulator API supply
  146. * management.
  147. */
  148. static const char *wm8994_main_supplies[] = {
  149. "DBVDD",
  150. "DCVDD",
  151. "AVDD1",
  152. "AVDD2",
  153. "CPVDD",
  154. "SPKVDD1",
  155. "SPKVDD2",
  156. };
  157. #ifdef CONFIG_PM
  158. static int wm8994_device_suspend(struct device *dev)
  159. {
  160. struct wm8994 *wm8994 = dev_get_drvdata(dev);
  161. int ret;
  162. /* GPIO configuration state is saved here since we may be configuring
  163. * the GPIO alternate functions even if we're not using the gpiolib
  164. * driver for them.
  165. */
  166. ret = wm8994_read(wm8994, WM8994_GPIO_1, WM8994_NUM_GPIO_REGS * 2,
  167. &wm8994->gpio_regs);
  168. if (ret < 0)
  169. dev_err(dev, "Failed to save GPIO registers: %d\n", ret);
  170. /* For similar reasons we also stash the regulator states */
  171. ret = wm8994_read(wm8994, WM8994_LDO_1, WM8994_NUM_LDO_REGS * 2,
  172. &wm8994->ldo_regs);
  173. if (ret < 0)
  174. dev_err(dev, "Failed to save LDO registers: %d\n", ret);
  175. ret = regulator_bulk_disable(ARRAY_SIZE(wm8994_main_supplies),
  176. wm8994->supplies);
  177. if (ret != 0) {
  178. dev_err(dev, "Failed to disable supplies: %d\n", ret);
  179. return ret;
  180. }
  181. return 0;
  182. }
  183. static int wm8994_device_resume(struct device *dev)
  184. {
  185. struct wm8994 *wm8994 = dev_get_drvdata(dev);
  186. int ret;
  187. ret = regulator_bulk_enable(ARRAY_SIZE(wm8994_main_supplies),
  188. wm8994->supplies);
  189. if (ret != 0) {
  190. dev_err(dev, "Failed to enable supplies: %d\n", ret);
  191. return ret;
  192. }
  193. ret = wm8994_write(wm8994, WM8994_LDO_1, WM8994_NUM_LDO_REGS * 2,
  194. &wm8994->ldo_regs);
  195. if (ret < 0)
  196. dev_err(dev, "Failed to restore LDO registers: %d\n", ret);
  197. ret = wm8994_write(wm8994, WM8994_GPIO_1, WM8994_NUM_GPIO_REGS * 2,
  198. &wm8994->gpio_regs);
  199. if (ret < 0)
  200. dev_err(dev, "Failed to restore GPIO registers: %d\n", ret);
  201. return 0;
  202. }
  203. #endif
  204. #ifdef CONFIG_REGULATOR
  205. static int wm8994_ldo_in_use(struct wm8994_pdata *pdata, int ldo)
  206. {
  207. struct wm8994_ldo_pdata *ldo_pdata;
  208. if (!pdata)
  209. return 0;
  210. ldo_pdata = &pdata->ldo[ldo];
  211. if (!ldo_pdata->init_data)
  212. return 0;
  213. return ldo_pdata->init_data->num_consumer_supplies != 0;
  214. }
  215. #else
  216. static int wm8994_ldo_in_use(struct wm8994_pdata *pdata, int ldo)
  217. {
  218. return 0;
  219. }
  220. #endif
  221. /*
  222. * Instantiate the generic non-control parts of the device.
  223. */
  224. static int wm8994_device_init(struct wm8994 *wm8994, unsigned long id, int irq)
  225. {
  226. struct wm8994_pdata *pdata = wm8994->dev->platform_data;
  227. int ret, i;
  228. mutex_init(&wm8994->io_lock);
  229. dev_set_drvdata(wm8994->dev, wm8994);
  230. /* Add the on-chip regulators first for bootstrapping */
  231. ret = mfd_add_devices(wm8994->dev, -1,
  232. wm8994_regulator_devs,
  233. ARRAY_SIZE(wm8994_regulator_devs),
  234. NULL, 0);
  235. if (ret != 0) {
  236. dev_err(wm8994->dev, "Failed to add children: %d\n", ret);
  237. goto err;
  238. }
  239. wm8994->supplies = kzalloc(sizeof(struct regulator_bulk_data) *
  240. ARRAY_SIZE(wm8994_main_supplies),
  241. GFP_KERNEL);
  242. if (!wm8994->supplies)
  243. goto err;
  244. for (i = 0; i < ARRAY_SIZE(wm8994_main_supplies); i++)
  245. wm8994->supplies[i].supply = wm8994_main_supplies[i];
  246. ret = regulator_bulk_get(wm8994->dev, ARRAY_SIZE(wm8994_main_supplies),
  247. wm8994->supplies);
  248. if (ret != 0) {
  249. dev_err(wm8994->dev, "Failed to get supplies: %d\n", ret);
  250. goto err_supplies;
  251. }
  252. ret = regulator_bulk_enable(ARRAY_SIZE(wm8994_main_supplies),
  253. wm8994->supplies);
  254. if (ret != 0) {
  255. dev_err(wm8994->dev, "Failed to enable supplies: %d\n", ret);
  256. goto err_get;
  257. }
  258. ret = wm8994_reg_read(wm8994, WM8994_SOFTWARE_RESET);
  259. if (ret < 0) {
  260. dev_err(wm8994->dev, "Failed to read ID register\n");
  261. goto err_enable;
  262. }
  263. if (ret != 0x8994) {
  264. dev_err(wm8994->dev, "Device is not a WM8994, ID is %x\n",
  265. ret);
  266. ret = -EINVAL;
  267. goto err_enable;
  268. }
  269. ret = wm8994_reg_read(wm8994, WM8994_CHIP_REVISION);
  270. if (ret < 0) {
  271. dev_err(wm8994->dev, "Failed to read revision register: %d\n",
  272. ret);
  273. goto err_enable;
  274. }
  275. switch (ret) {
  276. case 0:
  277. case 1:
  278. dev_warn(wm8994->dev, "revision %c not fully supported\n",
  279. 'A' + ret);
  280. break;
  281. default:
  282. dev_info(wm8994->dev, "revision %c\n", 'A' + ret);
  283. break;
  284. }
  285. if (pdata) {
  286. wm8994->gpio_base = pdata->gpio_base;
  287. /* GPIO configuration is only applied if it's non-zero */
  288. for (i = 0; i < ARRAY_SIZE(pdata->gpio_defaults); i++) {
  289. if (pdata->gpio_defaults[i]) {
  290. wm8994_set_bits(wm8994, WM8994_GPIO_1 + i,
  291. 0xffff,
  292. pdata->gpio_defaults[i]);
  293. }
  294. }
  295. }
  296. /* In some system designs where the regulators are not in use,
  297. * we can achieve a small reduction in leakage currents by
  298. * floating LDO outputs. This bit makes no difference if the
  299. * LDOs are enabled, it only affects cases where the LDOs were
  300. * in operation and are then disabled.
  301. */
  302. for (i = 0; i < WM8994_NUM_LDO_REGS; i++) {
  303. if (wm8994_ldo_in_use(pdata, i))
  304. wm8994_set_bits(wm8994, WM8994_LDO_1 + i,
  305. WM8994_LDO1_DISCH, WM8994_LDO1_DISCH);
  306. else
  307. wm8994_set_bits(wm8994, WM8994_LDO_1 + i,
  308. WM8994_LDO1_DISCH, 0);
  309. }
  310. ret = mfd_add_devices(wm8994->dev, -1,
  311. wm8994_devs, ARRAY_SIZE(wm8994_devs),
  312. NULL, 0);
  313. if (ret != 0) {
  314. dev_err(wm8994->dev, "Failed to add children: %d\n", ret);
  315. goto err_enable;
  316. }
  317. return 0;
  318. err_enable:
  319. regulator_bulk_disable(ARRAY_SIZE(wm8994_main_supplies),
  320. wm8994->supplies);
  321. err_get:
  322. regulator_bulk_free(ARRAY_SIZE(wm8994_main_supplies), wm8994->supplies);
  323. err_supplies:
  324. kfree(wm8994->supplies);
  325. err:
  326. mfd_remove_devices(wm8994->dev);
  327. kfree(wm8994);
  328. return ret;
  329. }
  330. static void wm8994_device_exit(struct wm8994 *wm8994)
  331. {
  332. mfd_remove_devices(wm8994->dev);
  333. regulator_bulk_disable(ARRAY_SIZE(wm8994_main_supplies),
  334. wm8994->supplies);
  335. regulator_bulk_free(ARRAY_SIZE(wm8994_main_supplies), wm8994->supplies);
  336. kfree(wm8994->supplies);
  337. kfree(wm8994);
  338. }
  339. static int wm8994_i2c_read_device(struct wm8994 *wm8994, unsigned short reg,
  340. int bytes, void *dest)
  341. {
  342. struct i2c_client *i2c = wm8994->control_data;
  343. int ret;
  344. u16 r = cpu_to_be16(reg);
  345. ret = i2c_master_send(i2c, (unsigned char *)&r, 2);
  346. if (ret < 0)
  347. return ret;
  348. if (ret != 2)
  349. return -EIO;
  350. ret = i2c_master_recv(i2c, dest, bytes);
  351. if (ret < 0)
  352. return ret;
  353. if (ret != bytes)
  354. return -EIO;
  355. return 0;
  356. }
  357. /* Currently we allocate the write buffer on the stack; this is OK for
  358. * small writes - if we need to do large writes this will need to be
  359. * revised.
  360. */
  361. static int wm8994_i2c_write_device(struct wm8994 *wm8994, unsigned short reg,
  362. int bytes, void *src)
  363. {
  364. struct i2c_client *i2c = wm8994->control_data;
  365. unsigned char msg[bytes + 2];
  366. int ret;
  367. reg = cpu_to_be16(reg);
  368. memcpy(&msg[0], &reg, 2);
  369. memcpy(&msg[2], src, bytes);
  370. ret = i2c_master_send(i2c, msg, bytes + 2);
  371. if (ret < 0)
  372. return ret;
  373. if (ret < bytes + 2)
  374. return -EIO;
  375. return 0;
  376. }
  377. static int wm8994_i2c_probe(struct i2c_client *i2c,
  378. const struct i2c_device_id *id)
  379. {
  380. struct wm8994 *wm8994;
  381. wm8994 = kzalloc(sizeof(struct wm8994), GFP_KERNEL);
  382. if (wm8994 == NULL) {
  383. kfree(i2c);
  384. return -ENOMEM;
  385. }
  386. i2c_set_clientdata(i2c, wm8994);
  387. wm8994->dev = &i2c->dev;
  388. wm8994->control_data = i2c;
  389. wm8994->read_dev = wm8994_i2c_read_device;
  390. wm8994->write_dev = wm8994_i2c_write_device;
  391. return wm8994_device_init(wm8994, id->driver_data, i2c->irq);
  392. }
  393. static int wm8994_i2c_remove(struct i2c_client *i2c)
  394. {
  395. struct wm8994 *wm8994 = i2c_get_clientdata(i2c);
  396. wm8994_device_exit(wm8994);
  397. return 0;
  398. }
  399. #ifdef CONFIG_PM
  400. static int wm8994_i2c_suspend(struct i2c_client *i2c, pm_message_t state)
  401. {
  402. return wm8994_device_suspend(&i2c->dev);
  403. }
  404. static int wm8994_i2c_resume(struct i2c_client *i2c)
  405. {
  406. return wm8994_device_resume(&i2c->dev);
  407. }
  408. #else
  409. #define wm8994_i2c_suspend NULL
  410. #define wm8994_i2c_resume NULL
  411. #endif
  412. static const struct i2c_device_id wm8994_i2c_id[] = {
  413. { "wm8994", 0 },
  414. { }
  415. };
  416. MODULE_DEVICE_TABLE(i2c, wm8994_i2c_id);
  417. static struct i2c_driver wm8994_i2c_driver = {
  418. .driver = {
  419. .name = "wm8994",
  420. .owner = THIS_MODULE,
  421. },
  422. .probe = wm8994_i2c_probe,
  423. .remove = wm8994_i2c_remove,
  424. .suspend = wm8994_i2c_suspend,
  425. .resume = wm8994_i2c_resume,
  426. .id_table = wm8994_i2c_id,
  427. };
  428. static int __init wm8994_i2c_init(void)
  429. {
  430. int ret;
  431. ret = i2c_add_driver(&wm8994_i2c_driver);
  432. if (ret != 0)
  433. pr_err("Failed to register wm8994 I2C driver: %d\n", ret);
  434. return ret;
  435. }
  436. module_init(wm8994_i2c_init);
  437. static void __exit wm8994_i2c_exit(void)
  438. {
  439. i2c_del_driver(&wm8994_i2c_driver);
  440. }
  441. module_exit(wm8994_i2c_exit);
  442. MODULE_DESCRIPTION("Core support for the WM8994 audio CODEC");
  443. MODULE_LICENSE("GPL");
  444. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");