wl1273-core.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. #include <linux/module.h>
  25. #define DRIVER_DESC "WL1273 FM Radio Core"
  26. static const struct i2c_device_id wl1273_driver_id_table[] = {
  27. { WL1273_FM_DRIVER_NAME, 0 },
  28. { }
  29. };
  30. MODULE_DEVICE_TABLE(i2c, wl1273_driver_id_table);
  31. static int wl1273_fm_read_reg(struct wl1273_core *core, u8 reg, u16 *value)
  32. {
  33. struct i2c_client *client = core->client;
  34. u8 b[2];
  35. int r;
  36. r = i2c_smbus_read_i2c_block_data(client, reg, sizeof(b), b);
  37. if (r != 2) {
  38. dev_err(&client->dev, "%s: Read: %d fails.\n", __func__, reg);
  39. return -EREMOTEIO;
  40. }
  41. *value = (u16)b[0] << 8 | b[1];
  42. return 0;
  43. }
  44. static int wl1273_fm_write_cmd(struct wl1273_core *core, u8 cmd, u16 param)
  45. {
  46. struct i2c_client *client = core->client;
  47. u8 buf[] = { (param >> 8) & 0xff, param & 0xff };
  48. int r;
  49. r = i2c_smbus_write_i2c_block_data(client, cmd, sizeof(buf), buf);
  50. if (r) {
  51. dev_err(&client->dev, "%s: Cmd: %d fails.\n", __func__, cmd);
  52. return r;
  53. }
  54. return 0;
  55. }
  56. static int wl1273_fm_write_data(struct wl1273_core *core, u8 *data, u16 len)
  57. {
  58. struct i2c_client *client = core->client;
  59. struct i2c_msg msg;
  60. int r;
  61. msg.addr = client->addr;
  62. msg.flags = 0;
  63. msg.buf = data;
  64. msg.len = len;
  65. r = i2c_transfer(client->adapter, &msg, 1);
  66. if (r != 1) {
  67. dev_err(&client->dev, "%s: write error.\n", __func__);
  68. return -EREMOTEIO;
  69. }
  70. return 0;
  71. }
  72. /**
  73. * wl1273_fm_set_audio() - Set audio mode.
  74. * @core: A pointer to the device struct.
  75. * @new_mode: The new audio mode.
  76. *
  77. * Audio modes are WL1273_AUDIO_DIGITAL and WL1273_AUDIO_ANALOG.
  78. */
  79. static int wl1273_fm_set_audio(struct wl1273_core *core, unsigned int new_mode)
  80. {
  81. int r = 0;
  82. if (core->mode == WL1273_MODE_OFF ||
  83. core->mode == WL1273_MODE_SUSPENDED)
  84. return -EPERM;
  85. if (core->mode == WL1273_MODE_RX && new_mode == WL1273_AUDIO_DIGITAL) {
  86. r = wl1273_fm_write_cmd(core, WL1273_PCM_MODE_SET,
  87. WL1273_PCM_DEF_MODE);
  88. if (r)
  89. goto out;
  90. r = wl1273_fm_write_cmd(core, WL1273_I2S_MODE_CONFIG_SET,
  91. core->i2s_mode);
  92. if (r)
  93. goto out;
  94. r = wl1273_fm_write_cmd(core, WL1273_AUDIO_ENABLE,
  95. WL1273_AUDIO_ENABLE_I2S);
  96. if (r)
  97. goto out;
  98. } else if (core->mode == WL1273_MODE_RX &&
  99. new_mode == WL1273_AUDIO_ANALOG) {
  100. r = wl1273_fm_write_cmd(core, WL1273_AUDIO_ENABLE,
  101. WL1273_AUDIO_ENABLE_ANALOG);
  102. if (r)
  103. goto out;
  104. } else if (core->mode == WL1273_MODE_TX &&
  105. new_mode == WL1273_AUDIO_DIGITAL) {
  106. r = wl1273_fm_write_cmd(core, WL1273_I2S_MODE_CONFIG_SET,
  107. core->i2s_mode);
  108. if (r)
  109. goto out;
  110. r = wl1273_fm_write_cmd(core, WL1273_AUDIO_IO_SET,
  111. WL1273_AUDIO_IO_SET_I2S);
  112. if (r)
  113. goto out;
  114. } else if (core->mode == WL1273_MODE_TX &&
  115. new_mode == WL1273_AUDIO_ANALOG) {
  116. r = wl1273_fm_write_cmd(core, WL1273_AUDIO_IO_SET,
  117. WL1273_AUDIO_IO_SET_ANALOG);
  118. if (r)
  119. goto out;
  120. }
  121. core->audio_mode = new_mode;
  122. out:
  123. return r;
  124. }
  125. /**
  126. * wl1273_fm_set_volume() - Set volume.
  127. * @core: A pointer to the device struct.
  128. * @volume: The new volume value.
  129. */
  130. static int wl1273_fm_set_volume(struct wl1273_core *core, unsigned int volume)
  131. {
  132. int r;
  133. if (volume > WL1273_MAX_VOLUME)
  134. return -EINVAL;
  135. if (core->volume == volume)
  136. return 0;
  137. r = wl1273_fm_write_cmd(core, WL1273_VOLUME_SET, volume);
  138. if (r)
  139. return r;
  140. core->volume = volume;
  141. return 0;
  142. }
  143. static int wl1273_core_remove(struct i2c_client *client)
  144. {
  145. dev_dbg(&client->dev, "%s\n", __func__);
  146. mfd_remove_devices(&client->dev);
  147. return 0;
  148. }
  149. static int wl1273_core_probe(struct i2c_client *client,
  150. const struct i2c_device_id *id)
  151. {
  152. struct wl1273_fm_platform_data *pdata = dev_get_platdata(&client->dev);
  153. struct wl1273_core *core;
  154. struct mfd_cell *cell;
  155. int children = 0;
  156. int r = 0;
  157. dev_dbg(&client->dev, "%s\n", __func__);
  158. if (!pdata) {
  159. dev_err(&client->dev, "No platform data.\n");
  160. return -EINVAL;
  161. }
  162. if (!(pdata->children & WL1273_RADIO_CHILD)) {
  163. dev_err(&client->dev, "Cannot function without radio child.\n");
  164. return -EINVAL;
  165. }
  166. core = devm_kzalloc(&client->dev, sizeof(*core), GFP_KERNEL);
  167. if (!core)
  168. return -ENOMEM;
  169. core->pdata = pdata;
  170. core->client = client;
  171. mutex_init(&core->lock);
  172. i2c_set_clientdata(client, core);
  173. dev_dbg(&client->dev, "%s: Have V4L2.\n", __func__);
  174. cell = &core->cells[children];
  175. cell->name = "wl1273_fm_radio";
  176. cell->platform_data = &core;
  177. cell->pdata_size = sizeof(core);
  178. children++;
  179. core->read = wl1273_fm_read_reg;
  180. core->write = wl1273_fm_write_cmd;
  181. core->write_data = wl1273_fm_write_data;
  182. core->set_audio = wl1273_fm_set_audio;
  183. core->set_volume = wl1273_fm_set_volume;
  184. if (pdata->children & WL1273_CODEC_CHILD) {
  185. cell = &core->cells[children];
  186. dev_dbg(&client->dev, "%s: Have codec.\n", __func__);
  187. cell->name = "wl1273-codec";
  188. cell->platform_data = &core;
  189. cell->pdata_size = sizeof(core);
  190. children++;
  191. }
  192. dev_dbg(&client->dev, "%s: number of children: %d.\n",
  193. __func__, children);
  194. r = mfd_add_devices(&client->dev, -1, core->cells,
  195. children, NULL, 0, NULL);
  196. if (r)
  197. goto err;
  198. return 0;
  199. err:
  200. pdata->free_resources();
  201. dev_dbg(&client->dev, "%s\n", __func__);
  202. return r;
  203. }
  204. static struct i2c_driver wl1273_core_driver = {
  205. .driver = {
  206. .name = WL1273_FM_DRIVER_NAME,
  207. },
  208. .probe = wl1273_core_probe,
  209. .id_table = wl1273_driver_id_table,
  210. .remove = wl1273_core_remove,
  211. };
  212. static int __init wl1273_core_init(void)
  213. {
  214. int r;
  215. r = i2c_add_driver(&wl1273_core_driver);
  216. if (r) {
  217. pr_err(WL1273_FM_DRIVER_NAME
  218. ": driver registration failed\n");
  219. return r;
  220. }
  221. return r;
  222. }
  223. static void __exit wl1273_core_exit(void)
  224. {
  225. i2c_del_driver(&wl1273_core_driver);
  226. }
  227. late_initcall(wl1273_core_init);
  228. module_exit(wl1273_core_exit);
  229. MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
  230. MODULE_DESCRIPTION(DRIVER_DESC);
  231. MODULE_LICENSE("GPL");