wm8994-core.c 12 KB

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