max9768.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * MAX9768 AMP driver
  3. *
  4. * Copyright (C) 2011, 2012 by Wolfram Sang, Pengutronix e.K.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; version 2 of the License.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/i2c.h>
  13. #include <linux/slab.h>
  14. #include <linux/gpio.h>
  15. #include <linux/regmap.h>
  16. #include <sound/core.h>
  17. #include <sound/soc.h>
  18. #include <sound/tlv.h>
  19. #include <sound/max9768.h>
  20. /* "Registers" */
  21. #define MAX9768_VOL 0
  22. #define MAX9768_CTRL 3
  23. /* Commands */
  24. #define MAX9768_CTRL_PWM 0x15
  25. #define MAX9768_CTRL_FILTERLESS 0x16
  26. struct max9768 {
  27. struct regmap *regmap;
  28. int mute_gpio;
  29. int shdn_gpio;
  30. u32 flags;
  31. };
  32. static struct reg_default max9768_default_regs[] = {
  33. { 0, 0 },
  34. { 3, MAX9768_CTRL_FILTERLESS},
  35. };
  36. static int max9768_get_gpio(struct snd_kcontrol *kcontrol,
  37. struct snd_ctl_elem_value *ucontrol)
  38. {
  39. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  40. struct max9768 *max9768 = snd_soc_codec_get_drvdata(codec);
  41. int val = gpio_get_value_cansleep(max9768->mute_gpio);
  42. ucontrol->value.integer.value[0] = !val;
  43. return 0;
  44. }
  45. static int max9768_set_gpio(struct snd_kcontrol *kcontrol,
  46. struct snd_ctl_elem_value *ucontrol)
  47. {
  48. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  49. struct max9768 *max9768 = snd_soc_codec_get_drvdata(codec);
  50. gpio_set_value_cansleep(max9768->mute_gpio, !ucontrol->value.integer.value[0]);
  51. return 0;
  52. }
  53. static const unsigned int volume_tlv[] = {
  54. TLV_DB_RANGE_HEAD(43),
  55. 0, 0, TLV_DB_SCALE_ITEM(-16150, 0, 0),
  56. 1, 1, TLV_DB_SCALE_ITEM(-9280, 0, 0),
  57. 2, 2, TLV_DB_SCALE_ITEM(-9030, 0, 0),
  58. 3, 3, TLV_DB_SCALE_ITEM(-8680, 0, 0),
  59. 4, 4, TLV_DB_SCALE_ITEM(-8430, 0, 0),
  60. 5, 5, TLV_DB_SCALE_ITEM(-8080, 0, 0),
  61. 6, 6, TLV_DB_SCALE_ITEM(-7830, 0, 0),
  62. 7, 7, TLV_DB_SCALE_ITEM(-7470, 0, 0),
  63. 8, 8, TLV_DB_SCALE_ITEM(-7220, 0, 0),
  64. 9, 9, TLV_DB_SCALE_ITEM(-6870, 0, 0),
  65. 10, 10, TLV_DB_SCALE_ITEM(-6620, 0, 0),
  66. 11, 11, TLV_DB_SCALE_ITEM(-6270, 0, 0),
  67. 12, 12, TLV_DB_SCALE_ITEM(-6020, 0, 0),
  68. 13, 13, TLV_DB_SCALE_ITEM(-5670, 0, 0),
  69. 14, 14, TLV_DB_SCALE_ITEM(-5420, 0, 0),
  70. 15, 17, TLV_DB_SCALE_ITEM(-5060, 250, 0),
  71. 18, 18, TLV_DB_SCALE_ITEM(-4370, 0, 0),
  72. 19, 19, TLV_DB_SCALE_ITEM(-4210, 0, 0),
  73. 20, 20, TLV_DB_SCALE_ITEM(-3960, 0, 0),
  74. 21, 21, TLV_DB_SCALE_ITEM(-3760, 0, 0),
  75. 22, 22, TLV_DB_SCALE_ITEM(-3600, 0, 0),
  76. 23, 23, TLV_DB_SCALE_ITEM(-3340, 0, 0),
  77. 24, 24, TLV_DB_SCALE_ITEM(-3150, 0, 0),
  78. 25, 25, TLV_DB_SCALE_ITEM(-2980, 0, 0),
  79. 26, 26, TLV_DB_SCALE_ITEM(-2720, 0, 0),
  80. 27, 27, TLV_DB_SCALE_ITEM(-2520, 0, 0),
  81. 28, 30, TLV_DB_SCALE_ITEM(-2350, 190, 0),
  82. 31, 31, TLV_DB_SCALE_ITEM(-1750, 0, 0),
  83. 32, 34, TLV_DB_SCALE_ITEM(-1640, 100, 0),
  84. 35, 37, TLV_DB_SCALE_ITEM(-1310, 110, 0),
  85. 38, 39, TLV_DB_SCALE_ITEM(-990, 100, 0),
  86. 40, 40, TLV_DB_SCALE_ITEM(-710, 0, 0),
  87. 41, 41, TLV_DB_SCALE_ITEM(-600, 0, 0),
  88. 42, 42, TLV_DB_SCALE_ITEM(-500, 0, 0),
  89. 43, 43, TLV_DB_SCALE_ITEM(-340, 0, 0),
  90. 44, 44, TLV_DB_SCALE_ITEM(-190, 0, 0),
  91. 45, 45, TLV_DB_SCALE_ITEM(-50, 0, 0),
  92. 46, 46, TLV_DB_SCALE_ITEM(50, 0, 0),
  93. 47, 50, TLV_DB_SCALE_ITEM(120, 40, 0),
  94. 51, 57, TLV_DB_SCALE_ITEM(290, 50, 0),
  95. 58, 58, TLV_DB_SCALE_ITEM(650, 0, 0),
  96. 59, 62, TLV_DB_SCALE_ITEM(700, 60, 0),
  97. 63, 63, TLV_DB_SCALE_ITEM(950, 0, 0),
  98. };
  99. static const struct snd_kcontrol_new max9768_volume[] = {
  100. SOC_SINGLE_TLV("Playback Volume", MAX9768_VOL, 0, 63, 0, volume_tlv),
  101. };
  102. static const struct snd_kcontrol_new max9768_mute[] = {
  103. SOC_SINGLE_BOOL_EXT("Playback Switch", 0, max9768_get_gpio, max9768_set_gpio),
  104. };
  105. static const struct snd_soc_dapm_widget max9768_dapm_widgets[] = {
  106. SND_SOC_DAPM_INPUT("IN"),
  107. SND_SOC_DAPM_OUTPUT("OUT+"),
  108. SND_SOC_DAPM_OUTPUT("OUT-"),
  109. };
  110. static const struct snd_soc_dapm_route max9768_dapm_routes[] = {
  111. { "OUT+", NULL, "IN" },
  112. { "OUT-", NULL, "IN" },
  113. };
  114. static int max9768_probe(struct snd_soc_codec *codec)
  115. {
  116. struct max9768 *max9768 = snd_soc_codec_get_drvdata(codec);
  117. int ret;
  118. codec->control_data = max9768->regmap;
  119. ret = snd_soc_codec_set_cache_io(codec, 2, 6, SND_SOC_REGMAP);
  120. if (ret)
  121. return ret;
  122. if (max9768->flags & MAX9768_FLAG_CLASSIC_PWM) {
  123. ret = snd_soc_write(codec, MAX9768_CTRL, MAX9768_CTRL_PWM);
  124. if (ret)
  125. return ret;
  126. }
  127. if (gpio_is_valid(max9768->mute_gpio)) {
  128. ret = snd_soc_add_codec_controls(codec, max9768_mute,
  129. ARRAY_SIZE(max9768_mute));
  130. if (ret)
  131. return ret;
  132. }
  133. return 0;
  134. }
  135. static struct snd_soc_codec_driver max9768_codec_driver = {
  136. .probe = max9768_probe,
  137. .controls = max9768_volume,
  138. .num_controls = ARRAY_SIZE(max9768_volume),
  139. .dapm_widgets = max9768_dapm_widgets,
  140. .num_dapm_widgets = ARRAY_SIZE(max9768_dapm_widgets),
  141. .dapm_routes = max9768_dapm_routes,
  142. .num_dapm_routes = ARRAY_SIZE(max9768_dapm_routes),
  143. };
  144. static const struct regmap_config max9768_i2c_regmap_config = {
  145. .reg_bits = 2,
  146. .val_bits = 6,
  147. .max_register = 3,
  148. .reg_defaults = max9768_default_regs,
  149. .num_reg_defaults = ARRAY_SIZE(max9768_default_regs),
  150. .cache_type = REGCACHE_RBTREE,
  151. };
  152. static int max9768_i2c_probe(struct i2c_client *client,
  153. const struct i2c_device_id *id)
  154. {
  155. struct max9768 *max9768;
  156. struct max9768_pdata *pdata = client->dev.platform_data;
  157. int err;
  158. max9768 = devm_kzalloc(&client->dev, sizeof(*max9768), GFP_KERNEL);
  159. if (!max9768)
  160. return -ENOMEM;
  161. if (pdata) {
  162. /* Mute on powerup to avoid clicks */
  163. err = gpio_request_one(pdata->mute_gpio, GPIOF_INIT_HIGH, "MAX9768 Mute");
  164. max9768->mute_gpio = err ?: pdata->mute_gpio;
  165. /* Activate chip by releasing shutdown, enables I2C */
  166. err = gpio_request_one(pdata->shdn_gpio, GPIOF_INIT_HIGH, "MAX9768 Shutdown");
  167. max9768->shdn_gpio = err ?: pdata->shdn_gpio;
  168. max9768->flags = pdata->flags;
  169. } else {
  170. max9768->shdn_gpio = -EINVAL;
  171. max9768->mute_gpio = -EINVAL;
  172. }
  173. i2c_set_clientdata(client, max9768);
  174. max9768->regmap = devm_regmap_init_i2c(client, &max9768_i2c_regmap_config);
  175. if (IS_ERR(max9768->regmap)) {
  176. err = PTR_ERR(max9768->regmap);
  177. goto err_gpio_free;
  178. }
  179. err = snd_soc_register_codec(&client->dev, &max9768_codec_driver, NULL, 0);
  180. if (err)
  181. goto err_gpio_free;
  182. return 0;
  183. err_gpio_free:
  184. if (gpio_is_valid(max9768->shdn_gpio))
  185. gpio_free(max9768->shdn_gpio);
  186. if (gpio_is_valid(max9768->mute_gpio))
  187. gpio_free(max9768->mute_gpio);
  188. return err;
  189. }
  190. static int max9768_i2c_remove(struct i2c_client *client)
  191. {
  192. struct max9768 *max9768 = i2c_get_clientdata(client);
  193. snd_soc_unregister_codec(&client->dev);
  194. if (gpio_is_valid(max9768->shdn_gpio))
  195. gpio_free(max9768->shdn_gpio);
  196. if (gpio_is_valid(max9768->mute_gpio))
  197. gpio_free(max9768->mute_gpio);
  198. return 0;
  199. }
  200. static const struct i2c_device_id max9768_i2c_id[] = {
  201. { "max9768", 0 },
  202. { }
  203. };
  204. MODULE_DEVICE_TABLE(i2c, max9768_i2c_id);
  205. static struct i2c_driver max9768_i2c_driver = {
  206. .driver = {
  207. .name = "max9768",
  208. .owner = THIS_MODULE,
  209. },
  210. .probe = max9768_i2c_probe,
  211. .remove = max9768_i2c_remove,
  212. .id_table = max9768_i2c_id,
  213. };
  214. module_i2c_driver(max9768_i2c_driver);
  215. MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
  216. MODULE_DESCRIPTION("ASoC MAX9768 amplifier driver");
  217. MODULE_LICENSE("GPL v2");