wl1273-core.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * MFD driver for wl1273 FM radio and audio codec submodules.
  3. *
  4. * Copyright (C) 2011 Nokia Corporation
  5. * Author: Matti Aaltonen <matti.j.aaltonen@nokia.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  19. * 02110-1301 USA
  20. *
  21. */
  22. #include <linux/mfd/wl1273-core.h>
  23. #include <linux/slab.h>
  24. #define DRIVER_DESC "WL1273 FM Radio Core"
  25. static const struct i2c_device_id wl1273_driver_id_table[] = {
  26. { WL1273_FM_DRIVER_NAME, 0 },
  27. { }
  28. };
  29. MODULE_DEVICE_TABLE(i2c, wl1273_driver_id_table);
  30. static int wl1273_fm_read_reg(struct wl1273_core *core, u8 reg, u16 *value)
  31. {
  32. struct i2c_client *client = core->client;
  33. u8 b[2];
  34. int r;
  35. r = i2c_smbus_read_i2c_block_data(client, reg, sizeof(b), b);
  36. if (r != 2) {
  37. dev_err(&client->dev, "%s: Read: %d fails.\n", __func__, reg);
  38. return -EREMOTEIO;
  39. }
  40. *value = (u16)b[0] << 8 | b[1];
  41. return 0;
  42. }
  43. static int wl1273_fm_write_cmd(struct wl1273_core *core, u8 cmd, u16 param)
  44. {
  45. struct i2c_client *client = core->client;
  46. u8 buf[] = { (param >> 8) & 0xff, param & 0xff };
  47. int r;
  48. r = i2c_smbus_write_i2c_block_data(client, cmd, sizeof(buf), buf);
  49. if (r) {
  50. dev_err(&client->dev, "%s: Cmd: %d fails.\n", __func__, cmd);
  51. return r;
  52. }
  53. return 0;
  54. }
  55. static int wl1273_fm_write_data(struct wl1273_core *core, u8 *data, u16 len)
  56. {
  57. struct i2c_client *client = core->client;
  58. struct i2c_msg msg;
  59. int r;
  60. msg.addr = client->addr;
  61. msg.flags = 0;
  62. msg.buf = data;
  63. msg.len = len;
  64. r = i2c_transfer(client->adapter, &msg, 1);
  65. if (r != 1) {
  66. dev_err(&client->dev, "%s: write error.\n", __func__);
  67. return -EREMOTEIO;
  68. }
  69. return 0;
  70. }
  71. /**
  72. * wl1273_fm_set_audio() - Set audio mode.
  73. * @core: A pointer to the device struct.
  74. * @new_mode: The new audio mode.
  75. *
  76. * Audio modes are WL1273_AUDIO_DIGITAL and WL1273_AUDIO_ANALOG.
  77. */
  78. static int wl1273_fm_set_audio(struct wl1273_core *core, unsigned int new_mode)
  79. {
  80. int r = 0;
  81. if (core->mode == WL1273_MODE_OFF ||
  82. core->mode == WL1273_MODE_SUSPENDED)
  83. return -EPERM;
  84. if (core->mode == WL1273_MODE_RX && new_mode == WL1273_AUDIO_DIGITAL) {
  85. r = wl1273_fm_write_cmd(core, WL1273_PCM_MODE_SET,
  86. WL1273_PCM_DEF_MODE);
  87. if (r)
  88. goto out;
  89. r = wl1273_fm_write_cmd(core, WL1273_I2S_MODE_CONFIG_SET,
  90. core->i2s_mode);
  91. if (r)
  92. goto out;
  93. r = wl1273_fm_write_cmd(core, WL1273_AUDIO_ENABLE,
  94. WL1273_AUDIO_ENABLE_I2S);
  95. if (r)
  96. goto out;
  97. } else if (core->mode == WL1273_MODE_RX &&
  98. new_mode == WL1273_AUDIO_ANALOG) {
  99. r = wl1273_fm_write_cmd(core, WL1273_AUDIO_ENABLE,
  100. WL1273_AUDIO_ENABLE_ANALOG);
  101. if (r)
  102. goto out;
  103. } else if (core->mode == WL1273_MODE_TX &&
  104. new_mode == WL1273_AUDIO_DIGITAL) {
  105. r = wl1273_fm_write_cmd(core, WL1273_I2S_MODE_CONFIG_SET,
  106. core->i2s_mode);
  107. if (r)
  108. goto out;
  109. r = wl1273_fm_write_cmd(core, WL1273_AUDIO_IO_SET,
  110. WL1273_AUDIO_IO_SET_I2S);
  111. if (r)
  112. goto out;
  113. } else if (core->mode == WL1273_MODE_TX &&
  114. new_mode == WL1273_AUDIO_ANALOG) {
  115. r = wl1273_fm_write_cmd(core, WL1273_AUDIO_IO_SET,
  116. WL1273_AUDIO_IO_SET_ANALOG);
  117. if (r)
  118. goto out;
  119. }
  120. core->audio_mode = new_mode;
  121. out:
  122. return r;
  123. }
  124. /**
  125. * wl1273_fm_set_volume() - Set volume.
  126. * @core: A pointer to the device struct.
  127. * @volume: The new volume value.
  128. */
  129. static int wl1273_fm_set_volume(struct wl1273_core *core, unsigned int volume)
  130. {
  131. int r;
  132. if (volume > WL1273_MAX_VOLUME)
  133. return -EINVAL;
  134. if (core->volume == volume)
  135. return 0;
  136. r = wl1273_fm_write_cmd(core, WL1273_VOLUME_SET, volume);
  137. if (r)
  138. return r;
  139. core->volume = volume;
  140. return 0;
  141. }
  142. static int wl1273_core_remove(struct i2c_client *client)
  143. {
  144. struct wl1273_core *core = i2c_get_clientdata(client);
  145. dev_dbg(&client->dev, "%s\n", __func__);
  146. mfd_remove_devices(&client->dev);
  147. kfree(core);
  148. return 0;
  149. }
  150. static int __devinit wl1273_core_probe(struct i2c_client *client,
  151. const struct i2c_device_id *id)
  152. {
  153. struct wl1273_fm_platform_data *pdata = client->dev.platform_data;
  154. struct wl1273_core *core;
  155. struct mfd_cell *cell;
  156. int children = 0;
  157. int r = 0;
  158. dev_dbg(&client->dev, "%s\n", __func__);
  159. if (!pdata) {
  160. dev_err(&client->dev, "No platform data.\n");
  161. return -EINVAL;
  162. }
  163. if (!(pdata->children & WL1273_RADIO_CHILD)) {
  164. dev_err(&client->dev, "Cannot function without radio child.\n");
  165. return -EINVAL;
  166. }
  167. core = kzalloc(sizeof(*core), GFP_KERNEL);
  168. if (!core)
  169. return -ENOMEM;
  170. core->pdata = pdata;
  171. core->client = client;
  172. mutex_init(&core->lock);
  173. i2c_set_clientdata(client, core);
  174. dev_dbg(&client->dev, "%s: Have V4L2.\n", __func__);
  175. cell = &core->cells[children];
  176. cell->name = "wl1273_fm_radio";
  177. cell->platform_data = &core;
  178. cell->pdata_size = sizeof(core);
  179. children++;
  180. core->read = wl1273_fm_read_reg;
  181. core->write = wl1273_fm_write_cmd;
  182. core->write_data = wl1273_fm_write_data;
  183. core->set_audio = wl1273_fm_set_audio;
  184. core->set_volume = wl1273_fm_set_volume;
  185. if (pdata->children & WL1273_CODEC_CHILD) {
  186. cell = &core->cells[children];
  187. dev_dbg(&client->dev, "%s: Have codec.\n", __func__);
  188. cell->name = "wl1273-codec";
  189. cell->platform_data = &core;
  190. cell->pdata_size = sizeof(core);
  191. children++;
  192. }
  193. dev_dbg(&client->dev, "%s: number of children: %d.\n",
  194. __func__, children);
  195. r = mfd_add_devices(&client->dev, -1, core->cells,
  196. children, NULL, 0);
  197. if (r)
  198. goto err;
  199. return 0;
  200. err:
  201. pdata->free_resources();
  202. kfree(core);
  203. dev_dbg(&client->dev, "%s\n", __func__);
  204. return r;
  205. }
  206. static struct i2c_driver wl1273_core_driver = {
  207. .driver = {
  208. .name = WL1273_FM_DRIVER_NAME,
  209. },
  210. .probe = wl1273_core_probe,
  211. .id_table = wl1273_driver_id_table,
  212. .remove = __devexit_p(wl1273_core_remove),
  213. };
  214. static int __init wl1273_core_init(void)
  215. {
  216. int r;
  217. r = i2c_add_driver(&wl1273_core_driver);
  218. if (r) {
  219. pr_err(WL1273_FM_DRIVER_NAME
  220. ": driver registration failed\n");
  221. return r;
  222. }
  223. return r;
  224. }
  225. static void __exit wl1273_core_exit(void)
  226. {
  227. i2c_del_driver(&wl1273_core_driver);
  228. }
  229. late_initcall(wl1273_core_init);
  230. module_exit(wl1273_core_exit);
  231. MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
  232. MODULE_DESCRIPTION(DRIVER_DESC);
  233. MODULE_LICENSE("GPL");