wm8994-core.c 14 KB

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