twl4030-audio.c 7.2 KB

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