wm8994-core.c 13 KB

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