wl1273-core.c 5.7 KB

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