wm8994-core.c 13 KB

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