wm8994-core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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 resource wm8994_codec_resources[] = {
  140. {
  141. .start = WM8994_IRQ_TEMP_SHUT,
  142. .end = WM8994_IRQ_TEMP_WARN,
  143. .flags = IORESOURCE_IRQ,
  144. },
  145. };
  146. static struct resource wm8994_gpio_resources[] = {
  147. {
  148. .start = WM8994_IRQ_GPIO(1),
  149. .end = WM8994_IRQ_GPIO(11),
  150. .flags = IORESOURCE_IRQ,
  151. },
  152. };
  153. static struct mfd_cell wm8994_devs[] = {
  154. {
  155. .name = "wm8994-codec",
  156. .num_resources = ARRAY_SIZE(wm8994_codec_resources),
  157. .resources = wm8994_codec_resources,
  158. },
  159. {
  160. .name = "wm8994-gpio",
  161. .num_resources = ARRAY_SIZE(wm8994_gpio_resources),
  162. .resources = wm8994_gpio_resources,
  163. },
  164. };
  165. /*
  166. * Supplies for the main bulk of CODEC; the LDO supplies are ignored
  167. * and should be handled via the standard regulator API supply
  168. * management.
  169. */
  170. static const char *wm8994_main_supplies[] = {
  171. "DBVDD",
  172. "DCVDD",
  173. "AVDD1",
  174. "AVDD2",
  175. "CPVDD",
  176. "SPKVDD1",
  177. "SPKVDD2",
  178. };
  179. static const char *wm8958_main_supplies[] = {
  180. "DBVDD1",
  181. "DBVDD2",
  182. "DBVDD3",
  183. "DCVDD",
  184. "AVDD1",
  185. "AVDD2",
  186. "CPVDD",
  187. "SPKVDD1",
  188. "SPKVDD2",
  189. };
  190. #ifdef CONFIG_PM
  191. static int wm8994_device_suspend(struct device *dev)
  192. {
  193. struct wm8994 *wm8994 = dev_get_drvdata(dev);
  194. int ret;
  195. /* GPIO configuration state is saved here since we may be configuring
  196. * the GPIO alternate functions even if we're not using the gpiolib
  197. * driver for them.
  198. */
  199. ret = wm8994_read(wm8994, WM8994_GPIO_1, WM8994_NUM_GPIO_REGS * 2,
  200. &wm8994->gpio_regs);
  201. if (ret < 0)
  202. dev_err(dev, "Failed to save GPIO registers: %d\n", ret);
  203. /* For similar reasons we also stash the regulator states */
  204. ret = wm8994_read(wm8994, WM8994_LDO_1, WM8994_NUM_LDO_REGS * 2,
  205. &wm8994->ldo_regs);
  206. if (ret < 0)
  207. dev_err(dev, "Failed to save LDO registers: %d\n", ret);
  208. ret = regulator_bulk_disable(wm8994->num_supplies,
  209. wm8994->supplies);
  210. if (ret != 0) {
  211. dev_err(dev, "Failed to disable supplies: %d\n", ret);
  212. return ret;
  213. }
  214. return 0;
  215. }
  216. static int wm8994_device_resume(struct device *dev)
  217. {
  218. struct wm8994 *wm8994 = dev_get_drvdata(dev);
  219. int ret;
  220. ret = regulator_bulk_enable(wm8994->num_supplies,
  221. wm8994->supplies);
  222. if (ret != 0) {
  223. dev_err(dev, "Failed to enable supplies: %d\n", ret);
  224. return ret;
  225. }
  226. ret = wm8994_write(wm8994, WM8994_INTERRUPT_STATUS_1_MASK,
  227. WM8994_NUM_IRQ_REGS * 2, &wm8994->irq_masks_cur);
  228. if (ret < 0)
  229. dev_err(dev, "Failed to restore interrupt masks: %d\n", ret);
  230. ret = wm8994_write(wm8994, WM8994_LDO_1, WM8994_NUM_LDO_REGS * 2,
  231. &wm8994->ldo_regs);
  232. if (ret < 0)
  233. dev_err(dev, "Failed to restore LDO registers: %d\n", ret);
  234. ret = wm8994_write(wm8994, WM8994_GPIO_1, WM8994_NUM_GPIO_REGS * 2,
  235. &wm8994->gpio_regs);
  236. if (ret < 0)
  237. dev_err(dev, "Failed to restore GPIO registers: %d\n", ret);
  238. return 0;
  239. }
  240. #endif
  241. #ifdef CONFIG_REGULATOR
  242. static int wm8994_ldo_in_use(struct wm8994_pdata *pdata, int ldo)
  243. {
  244. struct wm8994_ldo_pdata *ldo_pdata;
  245. if (!pdata)
  246. return 0;
  247. ldo_pdata = &pdata->ldo[ldo];
  248. if (!ldo_pdata->init_data)
  249. return 0;
  250. return ldo_pdata->init_data->num_consumer_supplies != 0;
  251. }
  252. #else
  253. static int wm8994_ldo_in_use(struct wm8994_pdata *pdata, int ldo)
  254. {
  255. return 0;
  256. }
  257. #endif
  258. /*
  259. * Instantiate the generic non-control parts of the device.
  260. */
  261. static int wm8994_device_init(struct wm8994 *wm8994, int irq)
  262. {
  263. struct wm8994_pdata *pdata = wm8994->dev->platform_data;
  264. const char *devname;
  265. int ret, i;
  266. mutex_init(&wm8994->io_lock);
  267. dev_set_drvdata(wm8994->dev, wm8994);
  268. /* Add the on-chip regulators first for bootstrapping */
  269. ret = mfd_add_devices(wm8994->dev, -1,
  270. wm8994_regulator_devs,
  271. ARRAY_SIZE(wm8994_regulator_devs),
  272. NULL, 0);
  273. if (ret != 0) {
  274. dev_err(wm8994->dev, "Failed to add children: %d\n", ret);
  275. goto err;
  276. }
  277. switch (wm8994->type) {
  278. case WM8994:
  279. wm8994->num_supplies = ARRAY_SIZE(wm8994_main_supplies);
  280. break;
  281. case WM8958:
  282. wm8994->num_supplies = ARRAY_SIZE(wm8958_main_supplies);
  283. break;
  284. default:
  285. BUG();
  286. return -EINVAL;
  287. }
  288. wm8994->supplies = kzalloc(sizeof(struct regulator_bulk_data) *
  289. wm8994->num_supplies,
  290. GFP_KERNEL);
  291. if (!wm8994->supplies) {
  292. ret = -ENOMEM;
  293. goto err;
  294. }
  295. switch (wm8994->type) {
  296. case WM8994:
  297. for (i = 0; i < ARRAY_SIZE(wm8994_main_supplies); i++)
  298. wm8994->supplies[i].supply = wm8994_main_supplies[i];
  299. break;
  300. case WM8958:
  301. for (i = 0; i < ARRAY_SIZE(wm8958_main_supplies); i++)
  302. wm8994->supplies[i].supply = wm8958_main_supplies[i];
  303. break;
  304. default:
  305. BUG();
  306. return -EINVAL;
  307. }
  308. ret = regulator_bulk_get(wm8994->dev, wm8994->num_supplies,
  309. wm8994->supplies);
  310. if (ret != 0) {
  311. dev_err(wm8994->dev, "Failed to get supplies: %d\n", ret);
  312. goto err_supplies;
  313. }
  314. ret = regulator_bulk_enable(wm8994->num_supplies,
  315. wm8994->supplies);
  316. if (ret != 0) {
  317. dev_err(wm8994->dev, "Failed to enable supplies: %d\n", ret);
  318. goto err_get;
  319. }
  320. ret = wm8994_reg_read(wm8994, WM8994_SOFTWARE_RESET);
  321. if (ret < 0) {
  322. dev_err(wm8994->dev, "Failed to read ID register\n");
  323. goto err_enable;
  324. }
  325. switch (ret) {
  326. case 0x8994:
  327. devname = "WM8994";
  328. if (wm8994->type != WM8994)
  329. dev_warn(wm8994->dev, "Device registered as type %d\n",
  330. wm8994->type);
  331. wm8994->type = WM8994;
  332. break;
  333. case 0x8958:
  334. devname = "WM8958";
  335. if (wm8994->type != WM8958)
  336. dev_warn(wm8994->dev, "Device registered as type %d\n",
  337. wm8994->type);
  338. wm8994->type = WM8958;
  339. break;
  340. default:
  341. dev_err(wm8994->dev, "Device is not a WM8994, ID is %x\n",
  342. ret);
  343. ret = -EINVAL;
  344. goto err_enable;
  345. }
  346. ret = wm8994_reg_read(wm8994, WM8994_CHIP_REVISION);
  347. if (ret < 0) {
  348. dev_err(wm8994->dev, "Failed to read revision register: %d\n",
  349. ret);
  350. goto err_enable;
  351. }
  352. switch (ret) {
  353. case 0:
  354. case 1:
  355. if (wm8994->type == WM8994)
  356. dev_warn(wm8994->dev,
  357. "revision %c not fully supported\n",
  358. 'A' + ret);
  359. break;
  360. default:
  361. break;
  362. }
  363. dev_info(wm8994->dev, "%s revision %c\n", devname, 'A' + ret);
  364. if (pdata) {
  365. wm8994->irq_base = pdata->irq_base;
  366. wm8994->gpio_base = pdata->gpio_base;
  367. /* GPIO configuration is only applied if it's non-zero */
  368. for (i = 0; i < ARRAY_SIZE(pdata->gpio_defaults); i++) {
  369. if (pdata->gpio_defaults[i]) {
  370. wm8994_set_bits(wm8994, WM8994_GPIO_1 + i,
  371. 0xffff,
  372. pdata->gpio_defaults[i]);
  373. }
  374. }
  375. }
  376. /* In some system designs where the regulators are not in use,
  377. * we can achieve a small reduction in leakage currents by
  378. * floating LDO outputs. This bit makes no difference if the
  379. * LDOs are enabled, it only affects cases where the LDOs were
  380. * in operation and are then disabled.
  381. */
  382. for (i = 0; i < WM8994_NUM_LDO_REGS; i++) {
  383. if (wm8994_ldo_in_use(pdata, i))
  384. wm8994_set_bits(wm8994, WM8994_LDO_1 + i,
  385. WM8994_LDO1_DISCH, WM8994_LDO1_DISCH);
  386. else
  387. wm8994_set_bits(wm8994, WM8994_LDO_1 + i,
  388. WM8994_LDO1_DISCH, 0);
  389. }
  390. wm8994_irq_init(wm8994);
  391. ret = mfd_add_devices(wm8994->dev, -1,
  392. wm8994_devs, ARRAY_SIZE(wm8994_devs),
  393. NULL, 0);
  394. if (ret != 0) {
  395. dev_err(wm8994->dev, "Failed to add children: %d\n", ret);
  396. goto err_irq;
  397. }
  398. return 0;
  399. err_irq:
  400. wm8994_irq_exit(wm8994);
  401. err_enable:
  402. regulator_bulk_disable(wm8994->num_supplies,
  403. wm8994->supplies);
  404. err_get:
  405. regulator_bulk_free(wm8994->num_supplies, wm8994->supplies);
  406. err_supplies:
  407. kfree(wm8994->supplies);
  408. err:
  409. mfd_remove_devices(wm8994->dev);
  410. kfree(wm8994);
  411. return ret;
  412. }
  413. static void wm8994_device_exit(struct wm8994 *wm8994)
  414. {
  415. mfd_remove_devices(wm8994->dev);
  416. wm8994_irq_exit(wm8994);
  417. regulator_bulk_disable(wm8994->num_supplies,
  418. wm8994->supplies);
  419. regulator_bulk_free(wm8994->num_supplies, wm8994->supplies);
  420. kfree(wm8994->supplies);
  421. kfree(wm8994);
  422. }
  423. static int wm8994_i2c_read_device(struct wm8994 *wm8994, unsigned short reg,
  424. int bytes, void *dest)
  425. {
  426. struct i2c_client *i2c = wm8994->control_data;
  427. int ret;
  428. u16 r = cpu_to_be16(reg);
  429. ret = i2c_master_send(i2c, (unsigned char *)&r, 2);
  430. if (ret < 0)
  431. return ret;
  432. if (ret != 2)
  433. return -EIO;
  434. ret = i2c_master_recv(i2c, dest, bytes);
  435. if (ret < 0)
  436. return ret;
  437. if (ret != bytes)
  438. return -EIO;
  439. return 0;
  440. }
  441. /* Currently we allocate the write buffer on the stack; this is OK for
  442. * small writes - if we need to do large writes this will need to be
  443. * revised.
  444. */
  445. static int wm8994_i2c_write_device(struct wm8994 *wm8994, unsigned short reg,
  446. int bytes, void *src)
  447. {
  448. struct i2c_client *i2c = wm8994->control_data;
  449. unsigned char msg[bytes + 2];
  450. int ret;
  451. reg = cpu_to_be16(reg);
  452. memcpy(&msg[0], &reg, 2);
  453. memcpy(&msg[2], src, bytes);
  454. ret = i2c_master_send(i2c, msg, bytes + 2);
  455. if (ret < 0)
  456. return ret;
  457. if (ret < bytes + 2)
  458. return -EIO;
  459. return 0;
  460. }
  461. static int wm8994_i2c_probe(struct i2c_client *i2c,
  462. const struct i2c_device_id *id)
  463. {
  464. struct wm8994 *wm8994;
  465. wm8994 = kzalloc(sizeof(struct wm8994), GFP_KERNEL);
  466. if (wm8994 == NULL)
  467. return -ENOMEM;
  468. i2c_set_clientdata(i2c, wm8994);
  469. wm8994->dev = &i2c->dev;
  470. wm8994->control_data = i2c;
  471. wm8994->read_dev = wm8994_i2c_read_device;
  472. wm8994->write_dev = wm8994_i2c_write_device;
  473. wm8994->irq = i2c->irq;
  474. wm8994->type = id->driver_data;
  475. return wm8994_device_init(wm8994, i2c->irq);
  476. }
  477. static int wm8994_i2c_remove(struct i2c_client *i2c)
  478. {
  479. struct wm8994 *wm8994 = i2c_get_clientdata(i2c);
  480. wm8994_device_exit(wm8994);
  481. return 0;
  482. }
  483. #ifdef CONFIG_PM
  484. static int wm8994_i2c_suspend(struct i2c_client *i2c, pm_message_t state)
  485. {
  486. return wm8994_device_suspend(&i2c->dev);
  487. }
  488. static int wm8994_i2c_resume(struct i2c_client *i2c)
  489. {
  490. return wm8994_device_resume(&i2c->dev);
  491. }
  492. #else
  493. #define wm8994_i2c_suspend NULL
  494. #define wm8994_i2c_resume NULL
  495. #endif
  496. static const struct i2c_device_id wm8994_i2c_id[] = {
  497. { "wm8994", WM8994 },
  498. { "wm8958", WM8958 },
  499. { }
  500. };
  501. MODULE_DEVICE_TABLE(i2c, wm8994_i2c_id);
  502. static struct i2c_driver wm8994_i2c_driver = {
  503. .driver = {
  504. .name = "wm8994",
  505. .owner = THIS_MODULE,
  506. },
  507. .probe = wm8994_i2c_probe,
  508. .remove = wm8994_i2c_remove,
  509. .suspend = wm8994_i2c_suspend,
  510. .resume = wm8994_i2c_resume,
  511. .id_table = wm8994_i2c_id,
  512. };
  513. static int __init wm8994_i2c_init(void)
  514. {
  515. int ret;
  516. ret = i2c_add_driver(&wm8994_i2c_driver);
  517. if (ret != 0)
  518. pr_err("Failed to register wm8994 I2C driver: %d\n", ret);
  519. return ret;
  520. }
  521. module_init(wm8994_i2c_init);
  522. static void __exit wm8994_i2c_exit(void)
  523. {
  524. i2c_del_driver(&wm8994_i2c_driver);
  525. }
  526. module_exit(wm8994_i2c_exit);
  527. MODULE_DESCRIPTION("Core support for the WM8994 audio CODEC");
  528. MODULE_LICENSE("GPL");
  529. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");