pm8xxx-spk.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* Copyright (c) 2012, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #define pr_fmt(fmt) "%s: " fmt, __func__
  13. #include <linux/module.h>
  14. #include <linux/err.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. #include <linux/stddef.h>
  18. #include <linux/debugfs.h>
  19. #include <linux/mfd/pm8xxx/core.h>
  20. #include <linux/mfd/pm8xxx/spk.h>
  21. #define PM8XXX_SPK_CTL1_REG_OFF 0
  22. #define PM8XXX_SPK_CTL2_REG_OFF 1
  23. #define PM8XXX_SPK_CTL3_REG_OFF 2
  24. #define PM8XXX_SPK_CTL4_REG_OFF 3
  25. #define PM8XXX_SPK_TEST_REG_1_OFF 4
  26. #define PM8XXX_SPK_TEST_REG_2_OFF 5
  27. #define PM8XXX_SPK_BANK_SEL 4
  28. #define PM8XXX_SPK_BANK_WRITE 0x80
  29. #define PM8XXX_SPK_BANK_VAL_MASK 0xF
  30. #define BOOST_6DB_GAIN_EN_MASK 0x8
  31. #define VSEL_LD0_1P1 0x0
  32. #define VSEL_LD0_1P2 0x2
  33. #define VSEL_LD0_1P0 0x4
  34. #define PWM_EN_MASK 0xF
  35. #define PM8XXX_SPK_TEST_REG_1_BANKS 8
  36. #define PM8XXX_SPK_TEST_REG_2_BANKS 2
  37. #define PM8XXX_SPK_GAIN 0x5
  38. #define PM8XXX_ADD_EN 0x1
  39. struct pm8xxx_spk_chip {
  40. struct list_head link;
  41. struct pm8xxx_spk_platform_data pdata;
  42. struct device *dev;
  43. enum pm8xxx_version version;
  44. struct mutex spk_mutex;
  45. u16 base;
  46. u16 end;
  47. };
  48. static struct pm8xxx_spk_chip *the_spk_chip;
  49. static inline bool spk_defined(void)
  50. {
  51. if (the_spk_chip == NULL || IS_ERR(the_spk_chip))
  52. return false;
  53. return true;
  54. }
  55. static int pm8xxx_spk_bank_write(u16 reg, u16 bank, u8 val)
  56. {
  57. int rc = 0;
  58. u8 bank_val = PM8XXX_SPK_BANK_WRITE | (bank << PM8XXX_SPK_BANK_SEL);
  59. bank_val |= (val & PM8XXX_SPK_BANK_VAL_MASK);
  60. mutex_lock(&the_spk_chip->spk_mutex);
  61. rc = pm8xxx_writeb(the_spk_chip->dev->parent, reg, bank_val);
  62. if (rc)
  63. pr_err("pm8xxx_writeb(): rc=%d\n", rc);
  64. mutex_unlock(&the_spk_chip->spk_mutex);
  65. return rc;
  66. }
  67. static int pm8xxx_spk_read(u16 addr)
  68. {
  69. int rc = 0;
  70. u8 val = 0;
  71. mutex_lock(&the_spk_chip->spk_mutex);
  72. rc = pm8xxx_readb(the_spk_chip->dev->parent,
  73. the_spk_chip->base + addr, &val);
  74. if (rc) {
  75. pr_err("pm8xxx_spk_readb() failed: rc=%d\n", rc);
  76. val = rc;
  77. }
  78. mutex_unlock(&the_spk_chip->spk_mutex);
  79. return val;
  80. }
  81. static int pm8xxx_spk_write(u16 addr, u8 val)
  82. {
  83. int rc = 0;
  84. mutex_lock(&the_spk_chip->spk_mutex);
  85. rc = pm8xxx_writeb(the_spk_chip->dev->parent,
  86. the_spk_chip->base + addr, val);
  87. if (rc)
  88. pr_err("pm8xxx_writeb() failed: rc=%d\n", rc);
  89. mutex_unlock(&the_spk_chip->spk_mutex);
  90. return rc;
  91. }
  92. int pm8xxx_spk_mute(bool mute)
  93. {
  94. u8 val = 0;
  95. int ret = 0;
  96. if (spk_defined() == false) {
  97. pr_err("Invalid spk handle or no spk_chip\n");
  98. return -ENODEV;
  99. }
  100. val = pm8xxx_spk_read(PM8XXX_SPK_CTL1_REG_OFF);
  101. val |= mute << 2;
  102. ret = pm8xxx_spk_write(PM8XXX_SPK_CTL1_REG_OFF, val);
  103. return ret;
  104. }
  105. EXPORT_SYMBOL_GPL(pm8xxx_spk_mute);
  106. int pm8xxx_spk_gain(u8 gain)
  107. {
  108. u8 val;
  109. int ret = 0;
  110. if (spk_defined() == false) {
  111. pr_err("Invalid spk handle or no spk_chip\n");
  112. return -ENODEV;
  113. }
  114. val = pm8xxx_spk_read(PM8XXX_SPK_CTL1_REG_OFF);
  115. val = (gain << 4) | (val & 0xF);
  116. ret = pm8xxx_spk_write(PM8XXX_SPK_CTL1_REG_OFF, val);
  117. if (!ret) {
  118. pm8xxx_spk_bank_write(the_spk_chip->base
  119. + PM8XXX_SPK_TEST_REG_1_OFF,
  120. 0, BOOST_6DB_GAIN_EN_MASK | VSEL_LD0_1P2);
  121. }
  122. return ret;
  123. }
  124. EXPORT_SYMBOL_GPL(pm8xxx_spk_gain);
  125. int pm8xxx_spk_enable(int enable)
  126. {
  127. int val = 0;
  128. u16 addr;
  129. int ret = 0;
  130. if (spk_defined() == false) {
  131. pr_err("Invalid spk handle or no spk_chip\n");
  132. return -ENODEV;
  133. }
  134. addr = the_spk_chip->base + PM8XXX_SPK_TEST_REG_1_OFF;
  135. val = pm8xxx_spk_read(PM8XXX_SPK_CTL1_REG_OFF);
  136. if (val < 0)
  137. return val;
  138. if (enable)
  139. val |= (1 << 3);
  140. else
  141. val &= ~(1 << 3);
  142. ret = pm8xxx_spk_write(PM8XXX_SPK_CTL1_REG_OFF, val);
  143. if (!ret)
  144. ret = pm8xxx_spk_bank_write(addr, 6, PWM_EN_MASK);
  145. return ret;
  146. }
  147. EXPORT_SYMBOL_GPL(pm8xxx_spk_enable);
  148. static int pm8xxx_spk_config(void)
  149. {
  150. u16 addr;
  151. int ret = 0;
  152. if (spk_defined() == false) {
  153. pr_err("Invalid spk handle or no spk_chip\n");
  154. return -ENODEV;
  155. }
  156. addr = the_spk_chip->base + PM8XXX_SPK_TEST_REG_1_OFF;
  157. ret = pm8xxx_spk_bank_write(addr, 6, PWM_EN_MASK & 0);
  158. if (!ret)
  159. ret = pm8xxx_spk_gain(PM8XXX_SPK_GAIN);
  160. return ret;
  161. }
  162. static int __devinit pm8xxx_spk_probe(struct platform_device *pdev)
  163. {
  164. const struct pm8xxx_spk_platform_data *pdata = pdev->dev.platform_data;
  165. int ret = 0;
  166. u8 value = 0;
  167. if (!pdata) {
  168. pr_err("missing platform data\n");
  169. return -EINVAL;
  170. }
  171. the_spk_chip = kzalloc(sizeof(struct pm8xxx_spk_chip), GFP_KERNEL);
  172. if (the_spk_chip == NULL) {
  173. pr_err("kzalloc() failed.\n");
  174. return -ENOMEM;
  175. }
  176. mutex_init(&the_spk_chip->spk_mutex);
  177. the_spk_chip->dev = &pdev->dev;
  178. the_spk_chip->version = pm8xxx_get_version(the_spk_chip->dev->parent);
  179. switch (pm8xxx_get_version(the_spk_chip->dev->parent)) {
  180. case PM8XXX_VERSION_8038:
  181. break;
  182. default:
  183. ret = -ENODEV;
  184. goto err_handle;
  185. }
  186. memcpy(&(the_spk_chip->pdata), pdata,
  187. sizeof(struct pm8xxx_spk_platform_data));
  188. the_spk_chip->base = pdev->resource[0].start;
  189. the_spk_chip->end = pdev->resource[0].end;
  190. if (the_spk_chip->pdata.spk_add_enable) {
  191. int val;
  192. val = pm8xxx_spk_read(PM8XXX_SPK_CTL1_REG_OFF);
  193. if (val < 0) {
  194. ret = val;
  195. goto err_handle;
  196. }
  197. val |= (the_spk_chip->pdata.spk_add_enable & PM8XXX_ADD_EN);
  198. ret = pm8xxx_spk_write(PM8XXX_SPK_CTL1_REG_OFF, val);
  199. if (ret < 0)
  200. goto err_handle;
  201. }
  202. value = ((the_spk_chip->pdata.cd_ng_threshold << 5) |
  203. the_spk_chip->pdata.cd_nf_preamp_bias << 3);
  204. pr_debug("Setting SPK_CTL2_REG = %02x\n", value);
  205. pm8xxx_spk_write(PM8XXX_SPK_CTL2_REG_OFF, value);
  206. value = ((the_spk_chip->pdata.cd_ng_hold << 5) |
  207. (the_spk_chip->pdata.cd_ng_max_atten << 1) |
  208. the_spk_chip->pdata.noise_mute);
  209. pr_debug("Setting SPK_CTL3_REG = %02x\n", value);
  210. pm8xxx_spk_write(PM8XXX_SPK_CTL3_REG_OFF, value);
  211. value = ((the_spk_chip->pdata.cd_ng_decay_rate << 5) |
  212. (the_spk_chip->pdata.cd_ng_attack_rate << 3) |
  213. the_spk_chip->pdata.cd_delay << 2);
  214. pr_debug("Setting SPK_CTL4_REG = %02x\n", value);
  215. pm8xxx_spk_write(PM8XXX_SPK_CTL4_REG_OFF, value);
  216. return pm8xxx_spk_config();
  217. err_handle:
  218. pr_err("pm8xxx_spk_probe failed."
  219. "Audio unavailable on speaker.\n");
  220. mutex_destroy(&the_spk_chip->spk_mutex);
  221. kfree(the_spk_chip);
  222. return ret;
  223. }
  224. static int __devexit pm8xxx_spk_remove(struct platform_device *pdev)
  225. {
  226. if (spk_defined() == false) {
  227. pr_err("Invalid spk handle or no spk_chip\n");
  228. return -ENODEV;
  229. }
  230. mutex_destroy(&the_spk_chip->spk_mutex);
  231. kfree(the_spk_chip);
  232. return 0;
  233. }
  234. static struct platform_driver pm8xxx_spk_driver = {
  235. .probe = pm8xxx_spk_probe,
  236. .remove = __devexit_p(pm8xxx_spk_remove),
  237. .driver = {
  238. .name = PM8XXX_SPK_DEV_NAME,
  239. .owner = THIS_MODULE,
  240. },
  241. };
  242. static int __init pm8xxx_spk_init(void)
  243. {
  244. return platform_driver_register(&pm8xxx_spk_driver);
  245. }
  246. subsys_initcall(pm8xxx_spk_init);
  247. static void __exit pm8xxx_spk_exit(void)
  248. {
  249. platform_driver_unregister(&pm8xxx_spk_driver);
  250. }
  251. module_exit(pm8xxx_spk_exit);
  252. MODULE_LICENSE("GPL v2");
  253. MODULE_DESCRIPTION("PM8XXX SPK driver");
  254. MODULE_ALIAS("platform:" PM8XXX_SPK_DEV_NAME);