twl4030-codec.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * MFD driver for twl4030 codec submodule
  3. *
  4. * Author: Peter Ujfalusi <peter.ujfalusi@nokia.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/kernel.h>
  26. #include <linux/fs.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/i2c/twl.h>
  29. #include <linux/mfd/core.h>
  30. #include <linux/mfd/twl4030-codec.h>
  31. #define TWL4030_CODEC_CELLS 2
  32. static struct platform_device *twl4030_codec_dev;
  33. struct twl4030_codec_resource {
  34. int request_count;
  35. u8 reg;
  36. u8 mask;
  37. };
  38. struct twl4030_codec {
  39. unsigned int audio_mclk;
  40. struct mutex mutex;
  41. struct twl4030_codec_resource resource[TWL4030_CODEC_RES_MAX];
  42. struct mfd_cell cells[TWL4030_CODEC_CELLS];
  43. };
  44. /*
  45. * Modify the resource, the function returns the content of the register
  46. * after the modification.
  47. */
  48. static int twl4030_codec_set_resource(enum twl4030_codec_res id, int enable)
  49. {
  50. struct twl4030_codec *codec = platform_get_drvdata(twl4030_codec_dev);
  51. u8 val;
  52. twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &val,
  53. codec->resource[id].reg);
  54. if (enable)
  55. val |= codec->resource[id].mask;
  56. else
  57. val &= ~codec->resource[id].mask;
  58. twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
  59. val, codec->resource[id].reg);
  60. return val;
  61. }
  62. static inline int twl4030_codec_get_resource(enum twl4030_codec_res id)
  63. {
  64. struct twl4030_codec *codec = platform_get_drvdata(twl4030_codec_dev);
  65. u8 val;
  66. twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &val,
  67. codec->resource[id].reg);
  68. return val;
  69. }
  70. /*
  71. * Enable the resource.
  72. * The function returns with error or the content of the register
  73. */
  74. int twl4030_codec_enable_resource(enum twl4030_codec_res id)
  75. {
  76. struct twl4030_codec *codec = platform_get_drvdata(twl4030_codec_dev);
  77. int val;
  78. if (id >= TWL4030_CODEC_RES_MAX) {
  79. dev_err(&twl4030_codec_dev->dev,
  80. "Invalid resource ID (%u)\n", id);
  81. return -EINVAL;
  82. }
  83. mutex_lock(&codec->mutex);
  84. if (!codec->resource[id].request_count)
  85. /* Resource was disabled, enable it */
  86. val = twl4030_codec_set_resource(id, 1);
  87. else
  88. val = twl4030_codec_get_resource(id);
  89. codec->resource[id].request_count++;
  90. mutex_unlock(&codec->mutex);
  91. return val;
  92. }
  93. EXPORT_SYMBOL_GPL(twl4030_codec_enable_resource);
  94. /*
  95. * Disable the resource.
  96. * The function returns with error or the content of the register
  97. */
  98. int twl4030_codec_disable_resource(unsigned id)
  99. {
  100. struct twl4030_codec *codec = platform_get_drvdata(twl4030_codec_dev);
  101. int val;
  102. if (id >= TWL4030_CODEC_RES_MAX) {
  103. dev_err(&twl4030_codec_dev->dev,
  104. "Invalid resource ID (%u)\n", id);
  105. return -EINVAL;
  106. }
  107. mutex_lock(&codec->mutex);
  108. if (!codec->resource[id].request_count) {
  109. dev_err(&twl4030_codec_dev->dev,
  110. "Resource has been disabled already (%u)\n", id);
  111. mutex_unlock(&codec->mutex);
  112. return -EPERM;
  113. }
  114. codec->resource[id].request_count--;
  115. if (!codec->resource[id].request_count)
  116. /* Resource can be disabled now */
  117. val = twl4030_codec_set_resource(id, 0);
  118. else
  119. val = twl4030_codec_get_resource(id);
  120. mutex_unlock(&codec->mutex);
  121. return val;
  122. }
  123. EXPORT_SYMBOL_GPL(twl4030_codec_disable_resource);
  124. unsigned int twl4030_codec_get_mclk(void)
  125. {
  126. struct twl4030_codec *codec = platform_get_drvdata(twl4030_codec_dev);
  127. return codec->audio_mclk;
  128. }
  129. EXPORT_SYMBOL_GPL(twl4030_codec_get_mclk);
  130. static int __devinit twl4030_codec_probe(struct platform_device *pdev)
  131. {
  132. struct twl4030_codec *codec;
  133. struct twl4030_codec_data *pdata = pdev->dev.platform_data;
  134. struct mfd_cell *cell = NULL;
  135. int ret, childs = 0;
  136. u8 val;
  137. if (!pdata) {
  138. dev_err(&pdev->dev, "Platform data is missing\n");
  139. return -EINVAL;
  140. }
  141. /* Configure APLL_INFREQ and disable APLL if enabled */
  142. val = 0;
  143. switch (pdata->audio_mclk) {
  144. case 19200000:
  145. val |= TWL4030_APLL_INFREQ_19200KHZ;
  146. break;
  147. case 26000000:
  148. val |= TWL4030_APLL_INFREQ_26000KHZ;
  149. break;
  150. case 38400000:
  151. val |= TWL4030_APLL_INFREQ_38400KHZ;
  152. break;
  153. default:
  154. dev_err(&pdev->dev, "Invalid audio_mclk\n");
  155. return -EINVAL;
  156. }
  157. twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
  158. val, TWL4030_REG_APLL_CTL);
  159. codec = kzalloc(sizeof(struct twl4030_codec), GFP_KERNEL);
  160. if (!codec)
  161. return -ENOMEM;
  162. platform_set_drvdata(pdev, codec);
  163. twl4030_codec_dev = pdev;
  164. mutex_init(&codec->mutex);
  165. codec->audio_mclk = pdata->audio_mclk;
  166. /* Codec power */
  167. codec->resource[TWL4030_CODEC_RES_POWER].reg = TWL4030_REG_CODEC_MODE;
  168. codec->resource[TWL4030_CODEC_RES_POWER].mask = TWL4030_CODECPDZ;
  169. /* PLL */
  170. codec->resource[TWL4030_CODEC_RES_APLL].reg = TWL4030_REG_APLL_CTL;
  171. codec->resource[TWL4030_CODEC_RES_APLL].mask = TWL4030_APLL_EN;
  172. if (pdata->audio) {
  173. cell = &codec->cells[childs];
  174. cell->name = "twl4030_codec_audio";
  175. cell->platform_data = pdata->audio;
  176. cell->data_size = sizeof(*pdata->audio);
  177. childs++;
  178. }
  179. if (pdata->vibra) {
  180. cell = &codec->cells[childs];
  181. cell->name = "twl4030_codec_vibra";
  182. cell->platform_data = pdata->vibra;
  183. cell->data_size = sizeof(*pdata->vibra);
  184. childs++;
  185. }
  186. if (childs)
  187. ret = mfd_add_devices(&pdev->dev, pdev->id, codec->cells,
  188. childs, NULL, 0);
  189. else {
  190. dev_err(&pdev->dev, "No platform data found for childs\n");
  191. ret = -ENODEV;
  192. }
  193. if (!ret)
  194. return 0;
  195. platform_set_drvdata(pdev, NULL);
  196. kfree(codec);
  197. twl4030_codec_dev = NULL;
  198. return ret;
  199. }
  200. static int __devexit twl4030_codec_remove(struct platform_device *pdev)
  201. {
  202. struct twl4030_codec *codec = platform_get_drvdata(pdev);
  203. mfd_remove_devices(&pdev->dev);
  204. platform_set_drvdata(pdev, NULL);
  205. kfree(codec);
  206. twl4030_codec_dev = NULL;
  207. return 0;
  208. }
  209. MODULE_ALIAS("platform:twl4030_codec");
  210. static struct platform_driver twl4030_codec_driver = {
  211. .probe = twl4030_codec_probe,
  212. .remove = __devexit_p(twl4030_codec_remove),
  213. .driver = {
  214. .owner = THIS_MODULE,
  215. .name = "twl4030_codec",
  216. },
  217. };
  218. static int __devinit twl4030_codec_init(void)
  219. {
  220. return platform_driver_register(&twl4030_codec_driver);
  221. }
  222. module_init(twl4030_codec_init);
  223. static void __devexit twl4030_codec_exit(void)
  224. {
  225. platform_driver_unregister(&twl4030_codec_driver);
  226. }
  227. module_exit(twl4030_codec_exit);
  228. MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@nokia.com>");
  229. MODULE_LICENSE("GPL");