wm8994-core.c 15 KB

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