wm8994-core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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. /* Explicitly put the device into reset in case regulators
  228. * don't get disabled in order to ensure consistent restart.
  229. */
  230. wm8994_reg_write(wm8994, WM8994_SOFTWARE_RESET, 0x8994);
  231. wm8994->suspended = true;
  232. ret = regulator_bulk_disable(wm8994->num_supplies,
  233. wm8994->supplies);
  234. if (ret != 0) {
  235. dev_err(dev, "Failed to disable supplies: %d\n", ret);
  236. return ret;
  237. }
  238. return 0;
  239. }
  240. static int wm8994_resume(struct device *dev)
  241. {
  242. struct wm8994 *wm8994 = dev_get_drvdata(dev);
  243. int ret;
  244. /* We may have lied to the PM core about suspending */
  245. if (!wm8994->suspended)
  246. return 0;
  247. ret = regulator_bulk_enable(wm8994->num_supplies,
  248. wm8994->supplies);
  249. if (ret != 0) {
  250. dev_err(dev, "Failed to enable supplies: %d\n", ret);
  251. return ret;
  252. }
  253. ret = wm8994_write(wm8994, WM8994_INTERRUPT_STATUS_1_MASK,
  254. WM8994_NUM_IRQ_REGS * 2, &wm8994->irq_masks_cur);
  255. if (ret < 0)
  256. dev_err(dev, "Failed to restore interrupt masks: %d\n", ret);
  257. ret = wm8994_write(wm8994, WM8994_LDO_1, WM8994_NUM_LDO_REGS * 2,
  258. &wm8994->ldo_regs);
  259. if (ret < 0)
  260. dev_err(dev, "Failed to restore LDO registers: %d\n", ret);
  261. ret = wm8994_write(wm8994, WM8994_GPIO_1, WM8994_NUM_GPIO_REGS * 2,
  262. &wm8994->gpio_regs);
  263. if (ret < 0)
  264. dev_err(dev, "Failed to restore GPIO registers: %d\n", ret);
  265. wm8994->suspended = false;
  266. return 0;
  267. }
  268. #endif
  269. #ifdef CONFIG_REGULATOR
  270. static int wm8994_ldo_in_use(struct wm8994_pdata *pdata, int ldo)
  271. {
  272. struct wm8994_ldo_pdata *ldo_pdata;
  273. if (!pdata)
  274. return 0;
  275. ldo_pdata = &pdata->ldo[ldo];
  276. if (!ldo_pdata->init_data)
  277. return 0;
  278. return ldo_pdata->init_data->num_consumer_supplies != 0;
  279. }
  280. #else
  281. static int wm8994_ldo_in_use(struct wm8994_pdata *pdata, int ldo)
  282. {
  283. return 0;
  284. }
  285. #endif
  286. /*
  287. * Instantiate the generic non-control parts of the device.
  288. */
  289. static int wm8994_device_init(struct wm8994 *wm8994, int irq)
  290. {
  291. struct wm8994_pdata *pdata = wm8994->dev->platform_data;
  292. const char *devname;
  293. int ret, i;
  294. mutex_init(&wm8994->io_lock);
  295. dev_set_drvdata(wm8994->dev, wm8994);
  296. /* Add the on-chip regulators first for bootstrapping */
  297. ret = mfd_add_devices(wm8994->dev, -1,
  298. wm8994_regulator_devs,
  299. ARRAY_SIZE(wm8994_regulator_devs),
  300. NULL, 0);
  301. if (ret != 0) {
  302. dev_err(wm8994->dev, "Failed to add children: %d\n", ret);
  303. goto err;
  304. }
  305. switch (wm8994->type) {
  306. case WM8994:
  307. wm8994->num_supplies = ARRAY_SIZE(wm8994_main_supplies);
  308. break;
  309. case WM8958:
  310. wm8994->num_supplies = ARRAY_SIZE(wm8958_main_supplies);
  311. break;
  312. default:
  313. BUG();
  314. return -EINVAL;
  315. }
  316. wm8994->supplies = kzalloc(sizeof(struct regulator_bulk_data) *
  317. wm8994->num_supplies,
  318. GFP_KERNEL);
  319. if (!wm8994->supplies) {
  320. ret = -ENOMEM;
  321. goto err;
  322. }
  323. switch (wm8994->type) {
  324. case WM8994:
  325. for (i = 0; i < ARRAY_SIZE(wm8994_main_supplies); i++)
  326. wm8994->supplies[i].supply = wm8994_main_supplies[i];
  327. break;
  328. case WM8958:
  329. for (i = 0; i < ARRAY_SIZE(wm8958_main_supplies); i++)
  330. wm8994->supplies[i].supply = wm8958_main_supplies[i];
  331. break;
  332. default:
  333. BUG();
  334. return -EINVAL;
  335. }
  336. ret = regulator_bulk_get(wm8994->dev, wm8994->num_supplies,
  337. wm8994->supplies);
  338. if (ret != 0) {
  339. dev_err(wm8994->dev, "Failed to get supplies: %d\n", ret);
  340. goto err_supplies;
  341. }
  342. ret = regulator_bulk_enable(wm8994->num_supplies,
  343. wm8994->supplies);
  344. if (ret != 0) {
  345. dev_err(wm8994->dev, "Failed to enable supplies: %d\n", ret);
  346. goto err_get;
  347. }
  348. ret = wm8994_reg_read(wm8994, WM8994_SOFTWARE_RESET);
  349. if (ret < 0) {
  350. dev_err(wm8994->dev, "Failed to read ID register\n");
  351. goto err_enable;
  352. }
  353. switch (ret) {
  354. case 0x8994:
  355. devname = "WM8994";
  356. if (wm8994->type != WM8994)
  357. dev_warn(wm8994->dev, "Device registered as type %d\n",
  358. wm8994->type);
  359. wm8994->type = WM8994;
  360. break;
  361. case 0x8958:
  362. devname = "WM8958";
  363. if (wm8994->type != WM8958)
  364. dev_warn(wm8994->dev, "Device registered as type %d\n",
  365. wm8994->type);
  366. wm8994->type = WM8958;
  367. break;
  368. default:
  369. dev_err(wm8994->dev, "Device is not a WM8994, ID is %x\n",
  370. ret);
  371. ret = -EINVAL;
  372. goto err_enable;
  373. }
  374. ret = wm8994_reg_read(wm8994, WM8994_CHIP_REVISION);
  375. if (ret < 0) {
  376. dev_err(wm8994->dev, "Failed to read revision register: %d\n",
  377. ret);
  378. goto err_enable;
  379. }
  380. switch (ret) {
  381. case 0:
  382. case 1:
  383. if (wm8994->type == WM8994)
  384. dev_warn(wm8994->dev,
  385. "revision %c not fully supported\n",
  386. 'A' + ret);
  387. break;
  388. default:
  389. break;
  390. }
  391. dev_info(wm8994->dev, "%s revision %c\n", devname, 'A' + ret);
  392. if (pdata) {
  393. wm8994->irq_base = pdata->irq_base;
  394. wm8994->gpio_base = pdata->gpio_base;
  395. /* GPIO configuration is only applied if it's non-zero */
  396. for (i = 0; i < ARRAY_SIZE(pdata->gpio_defaults); i++) {
  397. if (pdata->gpio_defaults[i]) {
  398. wm8994_set_bits(wm8994, WM8994_GPIO_1 + i,
  399. 0xffff,
  400. pdata->gpio_defaults[i]);
  401. }
  402. }
  403. }
  404. /* In some system designs where the regulators are not in use,
  405. * we can achieve a small reduction in leakage currents by
  406. * floating LDO outputs. This bit makes no difference if the
  407. * LDOs are enabled, it only affects cases where the LDOs were
  408. * in operation and are then disabled.
  409. */
  410. for (i = 0; i < WM8994_NUM_LDO_REGS; i++) {
  411. if (wm8994_ldo_in_use(pdata, i))
  412. wm8994_set_bits(wm8994, WM8994_LDO_1 + i,
  413. WM8994_LDO1_DISCH, WM8994_LDO1_DISCH);
  414. else
  415. wm8994_set_bits(wm8994, WM8994_LDO_1 + i,
  416. WM8994_LDO1_DISCH, 0);
  417. }
  418. wm8994_irq_init(wm8994);
  419. ret = mfd_add_devices(wm8994->dev, -1,
  420. wm8994_devs, ARRAY_SIZE(wm8994_devs),
  421. NULL, 0);
  422. if (ret != 0) {
  423. dev_err(wm8994->dev, "Failed to add children: %d\n", ret);
  424. goto err_irq;
  425. }
  426. pm_runtime_enable(wm8994->dev);
  427. pm_runtime_resume(wm8994->dev);
  428. return 0;
  429. err_irq:
  430. wm8994_irq_exit(wm8994);
  431. err_enable:
  432. regulator_bulk_disable(wm8994->num_supplies,
  433. wm8994->supplies);
  434. err_get:
  435. regulator_bulk_free(wm8994->num_supplies, wm8994->supplies);
  436. err_supplies:
  437. kfree(wm8994->supplies);
  438. err:
  439. mfd_remove_devices(wm8994->dev);
  440. kfree(wm8994);
  441. return ret;
  442. }
  443. static void wm8994_device_exit(struct wm8994 *wm8994)
  444. {
  445. pm_runtime_disable(wm8994->dev);
  446. mfd_remove_devices(wm8994->dev);
  447. wm8994_irq_exit(wm8994);
  448. regulator_bulk_disable(wm8994->num_supplies,
  449. wm8994->supplies);
  450. regulator_bulk_free(wm8994->num_supplies, wm8994->supplies);
  451. kfree(wm8994->supplies);
  452. kfree(wm8994);
  453. }
  454. static int wm8994_i2c_read_device(struct wm8994 *wm8994, unsigned short reg,
  455. int bytes, void *dest)
  456. {
  457. struct i2c_client *i2c = wm8994->control_data;
  458. int ret;
  459. u16 r = cpu_to_be16(reg);
  460. ret = i2c_master_send(i2c, (unsigned char *)&r, 2);
  461. if (ret < 0)
  462. return ret;
  463. if (ret != 2)
  464. return -EIO;
  465. ret = i2c_master_recv(i2c, dest, bytes);
  466. if (ret < 0)
  467. return ret;
  468. if (ret != bytes)
  469. return -EIO;
  470. return 0;
  471. }
  472. /* Currently we allocate the write buffer on the stack; this is OK for
  473. * small writes - if we need to do large writes this will need to be
  474. * revised.
  475. */
  476. static int wm8994_i2c_write_device(struct wm8994 *wm8994, unsigned short reg,
  477. int bytes, void *src)
  478. {
  479. struct i2c_client *i2c = wm8994->control_data;
  480. unsigned char msg[bytes + 2];
  481. int ret;
  482. reg = cpu_to_be16(reg);
  483. memcpy(&msg[0], &reg, 2);
  484. memcpy(&msg[2], src, bytes);
  485. ret = i2c_master_send(i2c, msg, bytes + 2);
  486. if (ret < 0)
  487. return ret;
  488. if (ret < bytes + 2)
  489. return -EIO;
  490. return 0;
  491. }
  492. static int wm8994_i2c_probe(struct i2c_client *i2c,
  493. const struct i2c_device_id *id)
  494. {
  495. struct wm8994 *wm8994;
  496. wm8994 = kzalloc(sizeof(struct wm8994), GFP_KERNEL);
  497. if (wm8994 == NULL)
  498. return -ENOMEM;
  499. i2c_set_clientdata(i2c, wm8994);
  500. wm8994->dev = &i2c->dev;
  501. wm8994->control_data = i2c;
  502. wm8994->read_dev = wm8994_i2c_read_device;
  503. wm8994->write_dev = wm8994_i2c_write_device;
  504. wm8994->irq = i2c->irq;
  505. wm8994->type = id->driver_data;
  506. return wm8994_device_init(wm8994, i2c->irq);
  507. }
  508. static int wm8994_i2c_remove(struct i2c_client *i2c)
  509. {
  510. struct wm8994 *wm8994 = i2c_get_clientdata(i2c);
  511. wm8994_device_exit(wm8994);
  512. return 0;
  513. }
  514. static const struct i2c_device_id wm8994_i2c_id[] = {
  515. { "wm8994", WM8994 },
  516. { "wm8958", WM8958 },
  517. { }
  518. };
  519. MODULE_DEVICE_TABLE(i2c, wm8994_i2c_id);
  520. UNIVERSAL_DEV_PM_OPS(wm8994_pm_ops, wm8994_suspend, wm8994_resume, NULL);
  521. static struct i2c_driver wm8994_i2c_driver = {
  522. .driver = {
  523. .name = "wm8994",
  524. .owner = THIS_MODULE,
  525. .pm = &wm8994_pm_ops,
  526. },
  527. .probe = wm8994_i2c_probe,
  528. .remove = wm8994_i2c_remove,
  529. .id_table = wm8994_i2c_id,
  530. };
  531. static int __init wm8994_i2c_init(void)
  532. {
  533. int ret;
  534. ret = i2c_add_driver(&wm8994_i2c_driver);
  535. if (ret != 0)
  536. pr_err("Failed to register wm8994 I2C driver: %d\n", ret);
  537. return ret;
  538. }
  539. module_init(wm8994_i2c_init);
  540. static void __exit wm8994_i2c_exit(void)
  541. {
  542. i2c_del_driver(&wm8994_i2c_driver);
  543. }
  544. module_exit(wm8994_i2c_exit);
  545. MODULE_DESCRIPTION("Core support for the WM8994 audio CODEC");
  546. MODULE_LICENSE("GPL");
  547. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");