twl4030-codec.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * MFD driver for twl4030 codec submodule
  3. *
  4. * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
  5. *
  6. * Copyright: (C) 2009 Nokia Corporation
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/types.h>
  25. #include <linux/slab.h>
  26. #include <linux/kernel.h>
  27. #include <linux/fs.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/i2c/twl.h>
  30. #include <linux/mfd/core.h>
  31. #include <linux/mfd/twl4030-codec.h>
  32. #define TWL4030_CODEC_CELLS 2
  33. static struct platform_device *twl4030_codec_dev;
  34. struct twl4030_codec_resource {
  35. int request_count;
  36. u8 reg;
  37. u8 mask;
  38. };
  39. struct twl4030_codec {
  40. unsigned int audio_mclk;
  41. struct mutex mutex;
  42. struct twl4030_codec_resource resource[TWL4030_CODEC_RES_MAX];
  43. struct mfd_cell cells[TWL4030_CODEC_CELLS];
  44. };
  45. /*
  46. * Modify the resource, the function returns the content of the register
  47. * after the modification.
  48. */
  49. static int twl4030_codec_set_resource(enum twl4030_codec_res id, int enable)
  50. {
  51. struct twl4030_codec *codec = platform_get_drvdata(twl4030_codec_dev);
  52. u8 val;
  53. twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &val,
  54. codec->resource[id].reg);
  55. if (enable)
  56. val |= codec->resource[id].mask;
  57. else
  58. val &= ~codec->resource[id].mask;
  59. twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
  60. val, codec->resource[id].reg);
  61. return val;
  62. }
  63. static inline int twl4030_codec_get_resource(enum twl4030_codec_res id)
  64. {
  65. struct twl4030_codec *codec = platform_get_drvdata(twl4030_codec_dev);
  66. u8 val;
  67. twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &val,
  68. codec->resource[id].reg);
  69. return val;
  70. }
  71. /*
  72. * Enable the resource.
  73. * The function returns with error or the content of the register
  74. */
  75. int twl4030_codec_enable_resource(enum twl4030_codec_res id)
  76. {
  77. struct twl4030_codec *codec = platform_get_drvdata(twl4030_codec_dev);
  78. int val;
  79. if (id >= TWL4030_CODEC_RES_MAX) {
  80. dev_err(&twl4030_codec_dev->dev,
  81. "Invalid resource ID (%u)\n", id);
  82. return -EINVAL;
  83. }
  84. mutex_lock(&codec->mutex);
  85. if (!codec->resource[id].request_count)
  86. /* Resource was disabled, enable it */
  87. val = twl4030_codec_set_resource(id, 1);
  88. else
  89. val = twl4030_codec_get_resource(id);
  90. codec->resource[id].request_count++;
  91. mutex_unlock(&codec->mutex);
  92. return val;
  93. }
  94. EXPORT_SYMBOL_GPL(twl4030_codec_enable_resource);
  95. /*
  96. * Disable the resource.
  97. * The function returns with error or the content of the register
  98. */
  99. int twl4030_codec_disable_resource(unsigned id)
  100. {
  101. struct twl4030_codec *codec = platform_get_drvdata(twl4030_codec_dev);
  102. int val;
  103. if (id >= TWL4030_CODEC_RES_MAX) {
  104. dev_err(&twl4030_codec_dev->dev,
  105. "Invalid resource ID (%u)\n", id);
  106. return -EINVAL;
  107. }
  108. mutex_lock(&codec->mutex);
  109. if (!codec->resource[id].request_count) {
  110. dev_err(&twl4030_codec_dev->dev,
  111. "Resource has been disabled already (%u)\n", id);
  112. mutex_unlock(&codec->mutex);
  113. return -EPERM;
  114. }
  115. codec->resource[id].request_count--;
  116. if (!codec->resource[id].request_count)
  117. /* Resource can be disabled now */
  118. val = twl4030_codec_set_resource(id, 0);
  119. else
  120. val = twl4030_codec_get_resource(id);
  121. mutex_unlock(&codec->mutex);
  122. return val;
  123. }
  124. EXPORT_SYMBOL_GPL(twl4030_codec_disable_resource);
  125. unsigned int twl4030_codec_get_mclk(void)
  126. {
  127. struct twl4030_codec *codec = platform_get_drvdata(twl4030_codec_dev);
  128. return codec->audio_mclk;
  129. }
  130. EXPORT_SYMBOL_GPL(twl4030_codec_get_mclk);
  131. static int __devinit twl4030_codec_probe(struct platform_device *pdev)
  132. {
  133. struct twl4030_codec *codec;
  134. struct twl4030_codec_data *pdata = pdev->dev.platform_data;
  135. struct mfd_cell *cell = NULL;
  136. int ret, childs = 0;
  137. u8 val;
  138. if (!pdata) {
  139. dev_err(&pdev->dev, "Platform data is missing\n");
  140. return -EINVAL;
  141. }
  142. /* Configure APLL_INFREQ and disable APLL if enabled */
  143. val = 0;
  144. switch (pdata->audio_mclk) {
  145. case 19200000:
  146. val |= TWL4030_APLL_INFREQ_19200KHZ;
  147. break;
  148. case 26000000:
  149. val |= TWL4030_APLL_INFREQ_26000KHZ;
  150. break;
  151. case 38400000:
  152. val |= TWL4030_APLL_INFREQ_38400KHZ;
  153. break;
  154. default:
  155. dev_err(&pdev->dev, "Invalid audio_mclk\n");
  156. return -EINVAL;
  157. }
  158. twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
  159. val, TWL4030_REG_APLL_CTL);
  160. codec = kzalloc(sizeof(struct twl4030_codec), GFP_KERNEL);
  161. if (!codec)
  162. return -ENOMEM;
  163. platform_set_drvdata(pdev, codec);
  164. twl4030_codec_dev = pdev;
  165. mutex_init(&codec->mutex);
  166. codec->audio_mclk = pdata->audio_mclk;
  167. /* Codec power */
  168. codec->resource[TWL4030_CODEC_RES_POWER].reg = TWL4030_REG_CODEC_MODE;
  169. codec->resource[TWL4030_CODEC_RES_POWER].mask = TWL4030_CODECPDZ;
  170. /* PLL */
  171. codec->resource[TWL4030_CODEC_RES_APLL].reg = TWL4030_REG_APLL_CTL;
  172. codec->resource[TWL4030_CODEC_RES_APLL].mask = TWL4030_APLL_EN;
  173. if (pdata->audio) {
  174. cell = &codec->cells[childs];
  175. cell->name = "twl4030-codec";
  176. cell->platform_data = pdata->audio;
  177. cell->pdata_size = sizeof(*pdata->audio);
  178. childs++;
  179. }
  180. if (pdata->vibra) {
  181. cell = &codec->cells[childs];
  182. cell->name = "twl4030-vibra";
  183. cell->platform_data = pdata->vibra;
  184. cell->pdata_size = sizeof(*pdata->vibra);
  185. childs++;
  186. }
  187. if (childs)
  188. ret = mfd_add_devices(&pdev->dev, pdev->id, codec->cells,
  189. childs, NULL, 0);
  190. else {
  191. dev_err(&pdev->dev, "No platform data found for childs\n");
  192. ret = -ENODEV;
  193. }
  194. if (!ret)
  195. return 0;
  196. platform_set_drvdata(pdev, NULL);
  197. kfree(codec);
  198. twl4030_codec_dev = NULL;
  199. return ret;
  200. }
  201. static int __devexit twl4030_codec_remove(struct platform_device *pdev)
  202. {
  203. struct twl4030_codec *codec = platform_get_drvdata(pdev);
  204. mfd_remove_devices(&pdev->dev);
  205. platform_set_drvdata(pdev, NULL);
  206. kfree(codec);
  207. twl4030_codec_dev = NULL;
  208. return 0;
  209. }
  210. MODULE_ALIAS("platform:twl4030-audio");
  211. static struct platform_driver twl4030_codec_driver = {
  212. .probe = twl4030_codec_probe,
  213. .remove = __devexit_p(twl4030_codec_remove),
  214. .driver = {
  215. .owner = THIS_MODULE,
  216. .name = "twl4030-audio",
  217. },
  218. };
  219. static int __devinit twl4030_codec_init(void)
  220. {
  221. return platform_driver_register(&twl4030_codec_driver);
  222. }
  223. module_init(twl4030_codec_init);
  224. static void __devexit twl4030_codec_exit(void)
  225. {
  226. platform_driver_unregister(&twl4030_codec_driver);
  227. }
  228. module_exit(twl4030_codec_exit);
  229. MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
  230. MODULE_LICENSE("GPL");