max98505.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. /*
  2. * max98505.c -- ALSA SoC MAX98505 driver
  3. *
  4. * Copyright 2013-2014 Maxim Integrated Products
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/delay.h>
  11. #include <linux/i2c.h>
  12. #include <linux/module.h>
  13. #include <linux/regmap.h>
  14. #include <linux/slab.h>
  15. #include <sound/pcm.h>
  16. #include <sound/pcm_params.h>
  17. #include <sound/soc.h>
  18. #include <sound/tlv.h>
  19. #include <sound/max98505.h>
  20. #include "max98505.h"
  21. #include "maxim_dsm.h"
  22. #define SUPPORT_DEVICE_TREE
  23. #ifdef SUPPORT_DEVICE_TREE
  24. #include <linux/regulator/consumer.h>
  25. #endif
  26. static struct reg_default max98505_reg[] = {
  27. { 0x00, 0x00 }, /* Battery Voltage Data */
  28. { 0x01, 0x00 }, /* Boost Voltage Data */
  29. { 0x02, 0x00 }, /* Live Status0 */
  30. { 0x03, 0x00 }, /* Live Status1 */
  31. { 0x04, 0x00 }, /* Live Status2 */
  32. { 0x05, 0x00 }, /* State0 */
  33. { 0x06, 0x00 }, /* State1 */
  34. { 0x07, 0x00 }, /* State2 */
  35. { 0x08, 0x00 }, /* Flag0 */
  36. { 0x09, 0x00 }, /* Flag1 */
  37. { 0x0A, 0x00 }, /* Flag2 */
  38. { 0x0B, 0x00 }, /* IRQ Enable0 */
  39. { 0x0C, 0x00 }, /* IRQ Enable1 */
  40. { 0x0D, 0x00 }, /* IRQ Enable2 */
  41. { 0x0E, 0x00 }, /* IRQ Clear0 */
  42. { 0x0F, 0x00 }, /* IRQ Clear1 */
  43. { 0x10, 0x00 }, /* IRQ Clear2 */
  44. { 0x11, 0xC0 }, /* Map0 */
  45. { 0x12, 0x00 }, /* Map1 */
  46. { 0x13, 0x00 }, /* Map2 */
  47. { 0x14, 0xF0 }, /* Map3 */
  48. { 0x15, 0x00 }, /* Map4 */
  49. { 0x16, 0xAB }, /* Map5 */
  50. { 0x17, 0x89 }, /* Map6 */
  51. { 0x18, 0x00 }, /* Map7 */
  52. { 0x19, 0x00 }, /* Map8 */
  53. { 0x1A, 0x06 }, /* DAI Clock Mode 1 */
  54. { 0x1B, 0xC0 }, /* DAI Clock Mode 2 */
  55. { 0x1C, 0x00 }, /* DAI Clock Divider Denominator MSBs */
  56. { 0x1D, 0x00 }, /* DAI Clock Divider Denominator LSBs */
  57. { 0x1E, 0xF0 }, /* DAI Clock Divider Numerator MSBs */
  58. { 0x1F, 0x00 }, /* DAI Clock Divider Numerator LSBs */
  59. { 0x20, 0x50 }, /* Format */
  60. { 0x21, 0x00 }, /* TDM Slot Select */
  61. { 0x22, 0x00 }, /* DOUT Configuration VMON */
  62. { 0x23, 0x00 }, /* DOUT Configuration IMON */
  63. { 0x24, 0x00 }, /* DOUT Configuration VBAT */
  64. { 0x25, 0x00 }, /* DOUT Configuration VBST */
  65. { 0x26, 0x00 }, /* DOUT Configuration FLAG */
  66. { 0x27, 0xFF }, /* DOUT HiZ Configuration 1 */
  67. { 0x28, 0xFF }, /* DOUT HiZ Configuration 2 */
  68. { 0x29, 0xFF }, /* DOUT HiZ Configuration 3 */
  69. { 0x2A, 0xFF }, /* DOUT HiZ Configuration 4 */
  70. { 0x2B, 0x02 }, /* DOUT Drive Strength */
  71. { 0x2C, 0x90 }, /* Filters */
  72. { 0x2D, 0x00 }, /* Gain */
  73. { 0x2E, 0x02 }, /* Gain Ramping */
  74. { 0x2F, 0x00 }, /* Speaker Amplifier */
  75. { 0x30, 0x0A }, /* Threshold */
  76. { 0x31, 0x00 }, /* ALC Attack */
  77. { 0x32, 0x80 }, /* ALC Atten and Release */
  78. { 0x33, 0x00 }, /* ALC Infinite Hold Release */
  79. { 0x34, 0x92 }, /* ALC Configuration */
  80. { 0x35, 0x01 }, /* Boost Converter */
  81. { 0x36, 0x00 }, /* Block Enable */
  82. { 0x37, 0x00 }, /* Configuration */
  83. { 0x38, 0x00 }, /* Global Enable */
  84. { 0x3A, 0x00 }, /* Boost Limiter */
  85. { 0xFF, 0x50 }, /* Revision ID */
  86. };
  87. static bool max98505_volatile_register(struct device *dev, unsigned int reg)
  88. {
  89. switch (reg) {
  90. case MAX98505_R000_VBAT_DATA:
  91. case MAX98505_R001_VBST_DATA:
  92. case MAX98505_R002_LIVE_STATUS0:
  93. case MAX98505_R003_LIVE_STATUS1:
  94. case MAX98505_R004_LIVE_STATUS2:
  95. case MAX98505_R005_STATE0:
  96. case MAX98505_R006_STATE1:
  97. case MAX98505_R007_STATE2:
  98. case MAX98505_R008_FLAG0:
  99. case MAX98505_R009_FLAG1:
  100. case MAX98505_R00A_FLAG2:
  101. case MAX98505_R0FF_VERSION:
  102. return true;
  103. default:
  104. return false;
  105. }
  106. }
  107. static bool max98505_readable_register(struct device *dev, unsigned int reg)
  108. {
  109. switch (reg) {
  110. case MAX98505_R00E_IRQ_CLEAR0:
  111. case MAX98505_R00F_IRQ_CLEAR1:
  112. case MAX98505_R010_IRQ_CLEAR2:
  113. case MAX98505_R033_ALC_HOLD_RLS:
  114. return false;
  115. default:
  116. return true;
  117. }
  118. };
  119. #ifdef SUPPORT_DEVICE_TREE
  120. static int reg_set_optimum_mode_check(struct regulator *reg, int load_uA)
  121. {
  122. return (regulator_count_voltages(reg) > 0) ?
  123. regulator_set_optimum_mode(reg, load_uA) : 0;
  124. }
  125. static int max98505_regulator_config(struct i2c_client *i2c, bool pullup, bool on)
  126. {
  127. struct regulator *max98505_vcc_i2c;
  128. int rc;
  129. #define VCC_I2C_MIN_UV 1800000
  130. #define VCC_I2C_MAX_UV 1800000
  131. #define I2C_LOAD_UA 300000
  132. pr_info("%s: enter\n", __func__);
  133. if (pullup) {
  134. pr_info("%s: I2C PULL UP.\n", __func__);
  135. max98505_vcc_i2c = regulator_get(&i2c->dev, "vcc_i2c");
  136. if (IS_ERR(max98505_vcc_i2c)) {
  137. rc = PTR_ERR(max98505_vcc_i2c);
  138. pr_info("%s: regulator get failed rc=%d\n", __func__, rc);
  139. goto error_get_vtg_i2c;
  140. }
  141. if (regulator_count_voltages(max98505_vcc_i2c) > 0) {
  142. rc = regulator_set_voltage(max98505_vcc_i2c,
  143. VCC_I2C_MIN_UV, VCC_I2C_MAX_UV);
  144. if (rc) {
  145. pr_info("%s: regulator set_vtg failed rc=%d\n", __func__, rc);
  146. goto error_set_vtg_i2c;
  147. }
  148. }
  149. rc = reg_set_optimum_mode_check(max98505_vcc_i2c, I2C_LOAD_UA);
  150. if (rc < 0) {
  151. pr_info("%s: regulator vcc_i2c set_opt failed rc=%d\n", __func__, rc);
  152. goto error_reg_opt_i2c;
  153. }
  154. rc = regulator_enable(max98505_vcc_i2c);
  155. if (rc) {
  156. pr_info("%s: regulator vcc_i2c enable failed rc=%d\n", __func__, rc);
  157. goto error_reg_en_vcc_i2c;
  158. }
  159. }
  160. return 0;
  161. error_set_vtg_i2c:
  162. regulator_put(max98505_vcc_i2c);
  163. error_get_vtg_i2c:
  164. if (regulator_count_voltages(max98505_vcc_i2c) > 0)
  165. regulator_set_voltage(max98505_vcc_i2c, 0,
  166. VCC_I2C_MAX_UV);
  167. error_reg_en_vcc_i2c:
  168. if(pullup) reg_set_optimum_mode_check(max98505_vcc_i2c, 0);
  169. error_reg_opt_i2c:
  170. regulator_disable(max98505_vcc_i2c);
  171. return rc;
  172. }
  173. #endif
  174. #ifdef USE_REG_DUMP
  175. static void reg_dump(struct max98505_priv *max98505)
  176. {
  177. int val_l;
  178. int i, j;
  179. static const struct {
  180. int start;
  181. int count;
  182. } reg_table[] = {
  183. { 0x02, 0x03 },
  184. { 0x1A, 0x1F },
  185. { 0x3A, 0x01 },
  186. { 0x00, 0x00 }
  187. };
  188. i = 0;
  189. while (reg_table[i].count != 0) {
  190. for(j = 0; j < reg_table[i].count; j++) {
  191. int addr = j + reg_table[i].start;
  192. regmap_read(max98505->regmap, addr, &val_l);
  193. pr_info("%s: reg 0x%02X, val_l 0x%02X\n",
  194. __func__, addr, val_l);
  195. }
  196. i++;
  197. }
  198. }
  199. #endif
  200. static const unsigned int max98505_spk_tlv[] = {
  201. TLV_DB_RANGE_HEAD(1),
  202. 1, 31, TLV_DB_SCALE_ITEM(-600, 100, 0),
  203. };
  204. static int max98505_spk_vol_get(struct snd_kcontrol *kcontrol,
  205. struct snd_ctl_elem_value *ucontrol)
  206. {
  207. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  208. struct max98505_priv *max98505 = snd_soc_codec_get_drvdata(codec);
  209. ucontrol->value.integer.value[0] = max98505->volume;
  210. return 0;
  211. }
  212. static int max98505_spk_vol_put(struct snd_kcontrol *kcontrol,
  213. struct snd_ctl_elem_value *ucontrol)
  214. {
  215. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  216. struct max98505_priv *max98505 = snd_soc_codec_get_drvdata(codec);
  217. unsigned int sel = ucontrol->value.integer.value[0];
  218. regmap_update_bits(max98505->regmap, MAX98505_R02D_GAIN,
  219. M98505_SPK_GAIN_MASK, sel << M98505_SPK_GAIN_SHIFT);
  220. max98505->volume = sel;
  221. return 0;
  222. }
  223. static int max98505_reg_get(struct snd_kcontrol *kcontrol,
  224. struct snd_ctl_elem_value *ucontrol, unsigned int reg,
  225. unsigned int mask, unsigned int shift)
  226. {
  227. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  228. struct max98505_priv *max98505 = snd_soc_codec_get_drvdata(codec);
  229. int data;
  230. regmap_read(max98505->regmap, reg, &data);
  231. ucontrol->value.integer.value[0] =
  232. (data & mask) >> shift;
  233. return 0;
  234. }
  235. static int max98505_reg_put(struct snd_kcontrol *kcontrol,
  236. struct snd_ctl_elem_value *ucontrol, unsigned int reg,
  237. unsigned int mask, unsigned int shift)
  238. {
  239. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  240. struct max98505_priv *max98505 = snd_soc_codec_get_drvdata(codec);
  241. unsigned int sel = ucontrol->value.integer.value[0];
  242. regmap_update_bits(max98505->regmap, reg, mask, sel << shift);
  243. return 0;
  244. }
  245. static int max98505_spk_ramp_get(struct snd_kcontrol *kcontrol,
  246. struct snd_ctl_elem_value *ucontrol)
  247. {
  248. return max98505_reg_get(kcontrol, ucontrol, MAX98505_R02E_GAIN_RAMPING,
  249. M98505_SPK_RMP_EN_MASK, M98505_SPK_RMP_EN_SHIFT);
  250. }
  251. static int max98505_spk_ramp_put(struct snd_kcontrol *kcontrol,
  252. struct snd_ctl_elem_value *ucontrol)
  253. {
  254. return max98505_reg_put(kcontrol, ucontrol, MAX98505_R02E_GAIN_RAMPING,
  255. M98505_SPK_RMP_EN_MASK, M98505_SPK_RMP_EN_SHIFT);
  256. }
  257. static int max98505_spk_zcd_get(struct snd_kcontrol *kcontrol,
  258. struct snd_ctl_elem_value *ucontrol)
  259. {
  260. return max98505_reg_get(kcontrol, ucontrol, MAX98505_R02E_GAIN_RAMPING,
  261. M98505_SPK_ZCD_EN_MASK, M98505_SPK_ZCD_EN_SHIFT);
  262. }
  263. static int max98505_spk_zcd_put(struct snd_kcontrol *kcontrol,
  264. struct snd_ctl_elem_value *ucontrol)
  265. {
  266. return max98505_reg_put(kcontrol, ucontrol, MAX98505_R02E_GAIN_RAMPING,
  267. M98505_SPK_ZCD_EN_MASK, M98505_SPK_ZCD_EN_SHIFT);
  268. }
  269. static int max98505_alc_en_get(struct snd_kcontrol *kcontrol,
  270. struct snd_ctl_elem_value *ucontrol)
  271. {
  272. return max98505_reg_get(kcontrol, ucontrol, MAX98505_R030_THRESHOLD,
  273. M98505_ALC_EN_MASK, M98505_ALC_EN_SHIFT);
  274. }
  275. static int max98505_alc_en_put(struct snd_kcontrol *kcontrol,
  276. struct snd_ctl_elem_value *ucontrol)
  277. {
  278. return max98505_reg_put(kcontrol, ucontrol, MAX98505_R030_THRESHOLD,
  279. M98505_ALC_EN_MASK, M98505_ALC_EN_SHIFT);
  280. }
  281. static int max98505_alc_threshold_get(struct snd_kcontrol *kcontrol,
  282. struct snd_ctl_elem_value *ucontrol)
  283. {
  284. return max98505_reg_get(kcontrol, ucontrol, MAX98505_R030_THRESHOLD,
  285. M98505_ALC_TH_MASK, M98505_ALC_TH_SHIFT);
  286. }
  287. static int max98505_alc_threshold_put(struct snd_kcontrol *kcontrol,
  288. struct snd_ctl_elem_value *ucontrol)
  289. {
  290. return max98505_reg_put(kcontrol, ucontrol, MAX98505_R030_THRESHOLD,
  291. M98505_ALC_TH_MASK, M98505_ALC_TH_SHIFT);
  292. }
  293. static const char * max98505_boost_voltage_text[] = {"8.5V", "8.25V",
  294. "8.0V", "7.75V","7.5V","7.25V","7.0V","6.75V", "6.5V", "6.5V",
  295. "6.5V","6.5V","6.5V","6.5V","6.5V","6.5V"};
  296. static const struct soc_enum max98505_boost_voltage_enum =
  297. SOC_ENUM_SINGLE(MAX98505_R037_CONFIGURATION, M98505_BST_VOUT_SHIFT, 15,
  298. max98505_boost_voltage_text);
  299. static const struct snd_kcontrol_new max98505_snd_controls[] = {
  300. SOC_SINGLE_EXT_TLV("Speaker Volume", MAX98505_R02D_GAIN,
  301. M98505_SPK_GAIN_SHIFT, (1<<M98505_SPK_GAIN_WIDTH)-1, 0,
  302. max98505_spk_vol_get, max98505_spk_vol_put, max98505_spk_tlv),
  303. SOC_SINGLE_EXT("Speaker Ramp", 0, 0, 1, 0,
  304. max98505_spk_ramp_get, max98505_spk_ramp_put),
  305. SOC_SINGLE_EXT("Speaker ZCD", 0, 0, 1, 0,
  306. max98505_spk_zcd_get, max98505_spk_zcd_put),
  307. SOC_SINGLE_EXT("ALC Enable", 0, 0, 1, 0,
  308. max98505_alc_en_get, max98505_alc_en_put),
  309. SOC_SINGLE_EXT("ALC Threshold", 0, 0, (1<<M98505_ALC_TH_WIDTH)-1, 0,
  310. max98505_alc_threshold_get, max98505_alc_threshold_put),
  311. SOC_ENUM("Boost Output Voltage", max98505_boost_voltage_enum),
  312. };
  313. static int max98505_add_widgets(struct snd_soc_codec *codec)
  314. {
  315. int ret;
  316. ret = snd_soc_add_codec_controls(codec, max98505_snd_controls,
  317. ARRAY_SIZE(max98505_snd_controls));
  318. return 0;
  319. }
  320. /* codec sample rate and n/m dividers parameter table */
  321. static const struct {
  322. u32 rate;
  323. u8 sr;
  324. u32 divisors[3][2];
  325. } rate_table[] = {
  326. { 8000, 0, {{ 1, 375}, {5, 1764}, { 1, 384}}},
  327. {11025, 1, {{147, 40000}, {1, 256}, {147, 40960}}},
  328. {12000, 2, {{ 1, 250}, {5, 1176}, { 1, 256}}},
  329. {16000, 3, {{ 2, 375}, {5, 882}, { 1, 192}}},
  330. {22050, 4, {{147, 20000}, {1, 128}, {147, 20480}}},
  331. {24000, 5, {{ 1, 125}, {5, 588}, { 1, 128}}},
  332. {32000, 6, {{ 4, 375}, {5, 441}, { 1, 96}}},
  333. {44100, 7, {{147, 10000}, {1, 64}, {147, 10240}}},
  334. {48000, 8, {{ 2, 125}, {5, 294}, { 1, 64}}},
  335. };
  336. static inline int max98505_rate_value(int rate, int clock, u8 *value, int *n, int *m)
  337. {
  338. int ret = -EINVAL;
  339. int i;
  340. for (i = 0; i < ARRAY_SIZE(rate_table); i++) {
  341. if (rate_table[i].rate >= rate) {
  342. *value = rate_table[i].sr;
  343. *n = rate_table[i].divisors[clock][0];
  344. *m = rate_table[i].divisors[clock][1];
  345. ret = 0;
  346. break;
  347. }
  348. }
  349. pr_info("%s: sample rate is %d, returning %d\n", __func__, rate_table[i].rate, *value);
  350. return ret;
  351. }
  352. static int max98505_set_tdm_slot(struct snd_soc_dai *codec_dai,
  353. unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
  354. {
  355. pr_info("%s: tx_mask 0x%X, rx_mask 0x%X, slots %d, slot width %d\n",
  356. __func__, tx_mask, rx_mask, slots, slot_width);
  357. return 0;
  358. }
  359. static void max98505_set_slave(struct max98505_priv *max98505)
  360. {
  361. pr_info("%s: ENTER\n", __func__);
  362. /*
  363. * 1. use BCLK instead of MCLK
  364. */
  365. regmap_update_bits(max98505->regmap, MAX98505_R01A_DAI_CLK_MODE1,
  366. M98505_DAI_CLK_SOURCE_MASK, M98505_DAI_CLK_SOURCE_MASK);
  367. /*
  368. * 2. set DAI to slave mode
  369. */
  370. regmap_update_bits(max98505->regmap, MAX98505_R01B_DAI_CLK_MODE2,
  371. M98505_DAI_MAS_MASK, 0);
  372. /*
  373. * 3. set BLCKs to LRCLKs to 64
  374. */
  375. regmap_update_bits(max98505->regmap, MAX98505_R01B_DAI_CLK_MODE2,
  376. M98505_DAI_BSEL_MASK, M98505_DAI_BSEL_32);
  377. /*
  378. * 4. set VMON slots
  379. */
  380. regmap_update_bits(max98505->regmap, MAX98505_R022_DOUT_CFG_VMON,
  381. M98505_DAI_VMON_EN_MASK, M98505_DAI_VMON_EN_MASK);
  382. regmap_update_bits(max98505->regmap, MAX98505_R022_DOUT_CFG_VMON,
  383. M98505_DAI_VMON_SLOT_MASK, M98505_DAI_VMON_SLOT_00_01);
  384. /*
  385. * 5. set IMON slots
  386. */
  387. regmap_update_bits(max98505->regmap, MAX98505_R023_DOUT_CFG_IMON,
  388. M98505_DAI_IMON_EN_MASK, M98505_DAI_IMON_EN_MASK);
  389. regmap_update_bits(max98505->regmap, MAX98505_R023_DOUT_CFG_IMON,
  390. M98505_DAI_IMON_SLOT_MASK, M98505_DAI_IMON_SLOT_02_03);
  391. }
  392. static void max98505_set_master(struct max98505_priv *max98505)
  393. {
  394. pr_info("%s: ENTER\n", __func__);
  395. /*
  396. * 1. use MCLK for Left channel, right channel always BCLK
  397. */
  398. regmap_update_bits(max98505->regmap, MAX98505_R01A_DAI_CLK_MODE1,
  399. M98505_DAI_CLK_SOURCE_MASK, 0);
  400. /*
  401. * 2. set left channel DAI to master mode, right channel always slave
  402. */
  403. regmap_update_bits(max98505->regmap, MAX98505_R01B_DAI_CLK_MODE2,
  404. M98505_DAI_MAS_MASK, M98505_DAI_MAS_MASK);
  405. }
  406. static int max98505_dai_set_fmt(struct snd_soc_dai *codec_dai,
  407. unsigned int fmt)
  408. {
  409. struct snd_soc_codec *codec = codec_dai->codec;
  410. struct max98505_priv *max98505 = snd_soc_codec_get_drvdata(codec);
  411. struct max98505_cdata *cdata;
  412. unsigned int invert = 0;
  413. pr_info("%s: fmt 0x%08X\n", __func__, fmt);
  414. cdata = &max98505->dai[0];
  415. cdata->fmt = fmt;
  416. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  417. case SND_SOC_DAIFMT_CBS_CFS:
  418. max98505_set_slave(max98505);
  419. break;
  420. case SND_SOC_DAIFMT_CBM_CFM:
  421. max98505_set_master(max98505);
  422. break;
  423. case SND_SOC_DAIFMT_CBS_CFM:
  424. case SND_SOC_DAIFMT_CBM_CFS:
  425. default:
  426. dev_err(codec->dev, "DAI clock mode unsupported");
  427. return -EINVAL;
  428. }
  429. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  430. case SND_SOC_DAIFMT_I2S:
  431. pr_info("%s: set SND_SOC_DAIFMT_I2S\n", __func__);
  432. break;
  433. case SND_SOC_DAIFMT_LEFT_J:
  434. pr_info("%s: set SND_SOC_DAIFMT_LEFT_J\n", __func__);
  435. break;
  436. case SND_SOC_DAIFMT_DSP_A:
  437. pr_info("%s: set SND_SOC_DAIFMT_DSP_A\n", __func__);
  438. default:
  439. dev_err(codec->dev, "DAI format unsupported, fmt:0x%x skip", fmt);
  440. //return -EINVAL; //temp block
  441. }
  442. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  443. case SND_SOC_DAIFMT_NB_NF:
  444. break;
  445. case SND_SOC_DAIFMT_NB_IF:
  446. invert = M98505_DAI_WCI_MASK;
  447. break;
  448. case SND_SOC_DAIFMT_IB_NF:
  449. invert = M98505_DAI_BCI_MASK;
  450. break;
  451. case SND_SOC_DAIFMT_IB_IF:
  452. invert = M98505_DAI_BCI_MASK | M98505_DAI_WCI_MASK;
  453. break;
  454. default:
  455. dev_err(codec->dev, "DAI invert mode unsupported skip");
  456. //return -EINVAL;
  457. }
  458. regmap_update_bits(max98505->regmap, MAX98505_R020_FORMAT,
  459. M98505_DAI_BCI_MASK | M98505_DAI_BCI_MASK, invert);
  460. return 0;
  461. }
  462. static int max98505_set_bias_level(struct snd_soc_codec *codec,
  463. enum snd_soc_bias_level level)
  464. {
  465. codec->dapm.bias_level = level;
  466. return 0;
  467. }
  468. static int max98505_set_clock(struct max98505_priv *max98505, unsigned int rate)
  469. {
  470. unsigned int clock;
  471. unsigned int mdll;
  472. unsigned int n;
  473. unsigned int m;
  474. u8 dai_sr;
  475. switch (max98505->sysclk) {
  476. case 6000000:
  477. clock = 0;
  478. mdll = M98505_MDLL_MULT_MCLKx16;
  479. break;
  480. case 11289600:
  481. clock = 1;
  482. mdll = M98505_MDLL_MULT_MCLKx8;
  483. break;
  484. case 12000000:
  485. clock = 0;
  486. mdll = M98505_MDLL_MULT_MCLKx8;
  487. break;
  488. case 12288000:
  489. clock = 2;
  490. mdll = M98505_MDLL_MULT_MCLKx8;
  491. break;
  492. default:
  493. dev_info(max98505->codec->dev, "unsupported sysclk %d\n",
  494. max98505->sysclk);
  495. return -EINVAL;
  496. }
  497. if (max98505_rate_value(rate, clock, &dai_sr, &n, &m))
  498. return -EINVAL;
  499. /*
  500. * 1. set DAI_SR to correct LRCLK frequency
  501. */
  502. regmap_update_bits(max98505->regmap, MAX98505_R01B_DAI_CLK_MODE2,
  503. M98505_DAI_SR_MASK, dai_sr << M98505_DAI_SR_SHIFT);
  504. /*
  505. * 2. set DAI m divider
  506. */
  507. regmap_write(max98505->regmap, MAX98505_R01C_DAI_CLK_DIV_M_MSBS,
  508. m >> 8);
  509. regmap_write(max98505->regmap, MAX98505_R01D_DAI_CLK_DIV_M_LSBS,
  510. m & 0xFF);
  511. /*
  512. * 3. set DAI n divider
  513. */
  514. regmap_write(max98505->regmap, MAX98505_R01E_DAI_CLK_DIV_N_MSBS,
  515. n >> 8);
  516. regmap_write(max98505->regmap, MAX98505_R01F_DAI_CLK_DIV_N_LSBS,
  517. n & 0xFF);
  518. /*
  519. * 4. set MDLL
  520. */
  521. regmap_update_bits(max98505->regmap, MAX98505_R01A_DAI_CLK_MODE1,
  522. M98505_MDLL_MULT_MASK, mdll << M98505_MDLL_MULT_SHIFT);
  523. return 0;
  524. }
  525. static int max98505_dai_hw_params(struct snd_pcm_substream *substream,
  526. struct snd_pcm_hw_params *params,
  527. struct snd_soc_dai *dai)
  528. {
  529. struct snd_soc_codec *codec = dai->codec;
  530. struct max98505_priv *max98505 = snd_soc_codec_get_drvdata(codec);
  531. struct max98505_cdata *cdata;
  532. unsigned int rate;
  533. pr_info("%s: enter\n", __func__);
  534. cdata = &max98505->dai[0];
  535. rate = params_rate(params);
  536. switch (params_format(params)) {
  537. case SNDRV_PCM_FORMAT_S16_LE:
  538. pr_info("%s: set SNDRV_PCM_FORMAT_S16_LE\n", __func__);
  539. regmap_update_bits(max98505->regmap, MAX98505_R020_FORMAT,
  540. M98505_DAI_CHANSZ_MASK, M98505_DAI_CHANSZ_16);
  541. break;
  542. case SNDRV_PCM_FORMAT_S24_LE:
  543. pr_info("%s: set SNDRV_PCM_FORMAT_S24_LE\n", __func__);
  544. #ifdef RIVER
  545. regmap_update_bits(max98505->regmap, MAX98505_R020_FORMAT,
  546. M98505_DAI_CHANSZ_MASK, M98505_DAI_CHANSZ_32);
  547. pr_info("%s: (really set to 32 bits)\n", __func__);
  548. #else
  549. regmap_update_bits(max98505->regmap, MAX98505_R020_FORMAT,
  550. M98505_DAI_CHANSZ_MASK, M98505_DAI_CHANSZ_24);
  551. #endif
  552. break;
  553. case SNDRV_PCM_FORMAT_S32_LE:
  554. pr_info("%s: set SNDRV_PCM_FORMAT_S32_LE\n", __func__);
  555. regmap_update_bits(max98505->regmap, MAX98505_R020_FORMAT,
  556. M98505_DAI_CHANSZ_MASK, M98505_DAI_CHANSZ_32);
  557. break;
  558. default:
  559. pr_info("%s: format unsupported %d but skip", __func__, params_format(params));
  560. regmap_update_bits(max98505->regmap, MAX98505_R020_FORMAT,
  561. M98505_DAI_CHANSZ_MASK, M98505_DAI_CHANSZ_16);
  562. }
  563. return max98505_set_clock(max98505, rate);
  564. }
  565. static int max98505_dai_set_sysclk(struct snd_soc_dai *dai,
  566. int clk_id, unsigned int freq, int dir)
  567. {
  568. struct snd_soc_codec *codec = dai->codec;
  569. struct max98505_priv *max98505 = snd_soc_codec_get_drvdata(codec);
  570. pr_info("%s: clk_id %d, freq %d, dir %d\n", __func__, clk_id, freq, dir);
  571. max98505->sysclk = freq;
  572. return 0;
  573. }
  574. static int max98505_dai_digital_mute(struct snd_soc_dai *codec_dai, int mute)
  575. {
  576. struct max98505_priv *max98505 = snd_soc_codec_get_drvdata(codec_dai->codec);
  577. pr_info("%s: mute %d\n", __func__, mute);
  578. if (mute) {
  579. regmap_update_bits(max98505->regmap, MAX98505_R02D_GAIN,
  580. M98505_SPK_GAIN_MASK, 0x00);
  581. usleep_range(5000, 5000);
  582. regmap_update_bits(max98505->regmap, MAX98505_R038_GLOBAL_ENABLE,
  583. M98505_EN_MASK, 0x0);
  584. }
  585. else {
  586. regmap_update_bits(max98505->regmap, MAX98505_R02D_GAIN,
  587. M98505_SPK_GAIN_MASK, max98505->volume);
  588. regmap_update_bits(max98505->regmap, MAX98505_R036_BLOCK_ENABLE,
  589. M98505_BST_EN_MASK | M98505_SPK_EN_MASK |
  590. M98505_ADC_IMON_EN_MASK | M98505_ADC_VMON_EN_MASK,
  591. M98505_BST_EN_MASK | M98505_SPK_EN_MASK |
  592. M98505_ADC_IMON_EN_MASK | M98505_ADC_VMON_EN_MASK);
  593. regmap_write(max98505->regmap, MAX98505_R038_GLOBAL_ENABLE,
  594. M98505_EN_MASK);
  595. }
  596. #ifdef USE_REG_DUMP
  597. reg_dump(max98505);
  598. #endif
  599. return 0;
  600. }
  601. #define MAX98505_RATES SNDRV_PCM_RATE_8000_48000
  602. #define MAX98505_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
  603. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  604. static struct snd_soc_dai_ops max98505_dai_ops = {
  605. .set_sysclk = max98505_dai_set_sysclk,
  606. .set_fmt = max98505_dai_set_fmt,
  607. .set_tdm_slot = max98505_set_tdm_slot,
  608. .hw_params = max98505_dai_hw_params,
  609. .digital_mute = max98505_dai_digital_mute,
  610. };
  611. static struct snd_soc_dai_driver max98505_dai[] = {
  612. {
  613. .name = "max98505-aif1",
  614. .playback = {
  615. .stream_name = "HiFi Playback",
  616. .channels_min = 1,
  617. .channels_max = 2,
  618. .rates = SNDRV_PCM_RATE_8000_48000,
  619. .formats = MAX98505_FORMATS,
  620. },
  621. .capture = {
  622. .stream_name = "HiFi Capture",
  623. .channels_min = 1,
  624. .channels_max = 2,
  625. .rates = SNDRV_PCM_RATE_8000_48000,
  626. .formats = MAX98505_FORMATS,
  627. },
  628. .ops = &max98505_dai_ops,
  629. }
  630. };
  631. static void max98505_handle_pdata(struct snd_soc_codec *codec)
  632. {
  633. struct max98505_priv *max98505 = snd_soc_codec_get_drvdata(codec);
  634. struct max98505_pdata *pdata = max98505->pdata;
  635. if (!pdata) {
  636. dev_dbg(codec->dev, "No platform data\n");
  637. return;
  638. }
  639. }
  640. #ifdef CONFIG_PM
  641. static int max98505_suspend(struct snd_soc_codec *codec)
  642. {
  643. pr_info("%s: enter\n", __func__);
  644. return 0;
  645. }
  646. static int max98505_resume(struct snd_soc_codec *codec)
  647. {
  648. pr_info("%s: enter\n", __func__);
  649. return 0;
  650. }
  651. #else
  652. #define max98505_suspend NULL
  653. #define max98505_resume NULL
  654. #endif
  655. static int max98505_probe(struct snd_soc_codec *codec)
  656. {
  657. struct max98505_priv *max98505 = snd_soc_codec_get_drvdata(codec);
  658. struct max98505_cdata *cdata;
  659. int ret = 0;
  660. int reg = 0;
  661. dev_info(codec->dev, "MONO - built on %s at %s\n",
  662. __DATE__,
  663. __TIME__);
  664. dev_info(codec->dev, "build number %s\n", MAX98505_REVISION);
  665. max98505->codec = codec;
  666. codec->control_data = max98505->regmap;
  667. ret = snd_soc_codec_set_cache_io(codec, 8, 8, SND_SOC_I2C);
  668. if (ret != 0) {
  669. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  670. return ret;
  671. }
  672. max98505->sysclk = 12288000;
  673. max98505->volume = 0x07;
  674. cdata = &max98505->dai[0];
  675. cdata->rate = (unsigned)-1;
  676. cdata->fmt = (unsigned)-1;
  677. reg = 0;
  678. ret = regmap_read(max98505->regmap, MAX98505_R0FF_VERSION, &reg);
  679. if ((ret < 0) || ((reg != MAX98505_VERSION) && (reg != MAX98505_VERSION1) && (reg != MAX98505_VERSION2))) {
  680. dev_err(codec->dev,
  681. "device initialization error (%d 0x%02X)\n",
  682. ret,
  683. reg);
  684. goto err_access;
  685. }
  686. dev_info(codec->dev, "device version 0x%02X\n", reg);
  687. #if 0
  688. /* FOR DEBUGGING ONLY */
  689. regcache_cache_bypass(max98505->regmap, true);
  690. dev_info(codec->dev, "regmap cache bypass ENABLED!\n");
  691. /**********************/
  692. #endif
  693. regmap_write(max98505->regmap, MAX98505_R038_GLOBAL_ENABLE, 0x00);
  694. /* It's not the default but we need to set DAI_DLY */
  695. regmap_write(max98505->regmap, MAX98505_R020_FORMAT, M98505_DAI_DLY_MASK);
  696. regmap_write(max98505->regmap, MAX98505_R021_TDM_SLOT_SELECT, 0xC8);
  697. regmap_write(max98505->regmap, MAX98505_R027_DOUT_HIZ_CFG1, 0xFF);
  698. regmap_write(max98505->regmap, MAX98505_R028_DOUT_HIZ_CFG2, 0xFF);
  699. regmap_write(max98505->regmap, MAX98505_R029_DOUT_HIZ_CFG3, 0xFF);
  700. regmap_write(max98505->regmap, MAX98505_R02A_DOUT_HIZ_CFG4, 0xF0);
  701. regmap_write(max98505->regmap, MAX98505_R02C_FILTERS, 0xD8);
  702. // regmap_write(max98505->regmap, MAX98505_R034_ALC_CONFIGURATION, 0xF8);
  703. regmap_write(max98505->regmap, MAX98505_R034_ALC_CONFIGURATION, 0x12);
  704. /*****************************************************************/
  705. /* Set boost output to minimum until DSM is implemented */
  706. regmap_write(max98505->regmap, MAX98505_R037_CONFIGURATION, 0xF0);
  707. /*****************************************************************/
  708. // Disable ALC muting
  709. regmap_write(max98505->regmap, MAX98505_R03A_BOOST_LIMITER, 0xF8);
  710. regmap_update_bits(max98505->regmap, MAX98505_R02D_GAIN,
  711. M98505_DAC_IN_SEL_MASK, M98505_DAC_IN_SEL_DIV2_SUMMED_DAI);
  712. max98505_handle_pdata(codec);
  713. max98505_add_widgets(codec);
  714. ret = sysfs_create_group(&codec->dev->kobj, &maxim_attribute_group);
  715. if(ret) {
  716. pr_err("failed to create sysfs group [%d]", ret);
  717. }
  718. err_access:
  719. pr_info("%s: exit %d\n", __func__, ret);
  720. ret = 0; // temp
  721. return ret;
  722. }
  723. static int max98505_remove(struct snd_soc_codec *codec)
  724. {
  725. pr_info("%s: enter\n", __func__);
  726. return 0;
  727. }
  728. static struct snd_soc_codec_driver soc_codec_dev_max98505 = {
  729. .probe = max98505_probe,
  730. .remove = max98505_remove,
  731. .set_bias_level = max98505_set_bias_level,
  732. .suspend = max98505_suspend,
  733. .resume = max98505_resume,
  734. };
  735. static const struct regmap_config max98505_regmap = {
  736. .reg_bits = 8,
  737. .val_bits = 8,
  738. .max_register = MAX98505_R0FF_VERSION,
  739. .reg_defaults = max98505_reg,
  740. .num_reg_defaults = ARRAY_SIZE(max98505_reg),
  741. .volatile_reg = max98505_volatile_register,
  742. .readable_reg = max98505_readable_register,
  743. .cache_type = REGCACHE_RBTREE,
  744. };
  745. static int max98505_i2c_probe(struct i2c_client *i2c_l,
  746. const struct i2c_device_id *id)
  747. {
  748. struct max98505_priv *max98505;
  749. int ret;
  750. pr_info("%s: enter, device '%s'\n", __func__, id->name);
  751. #ifdef SUPPORT_DEVICE_TREE
  752. max98505_regulator_config(i2c_l, of_property_read_bool(i2c_l->dev.of_node,
  753. "max98505,i2c-pull-up"), 1);
  754. #endif
  755. max98505 = kzalloc(sizeof(struct max98505_priv), GFP_KERNEL);
  756. if (max98505 == NULL)
  757. return -ENOMEM;
  758. max98505->devtype = id->driver_data;
  759. i2c_set_clientdata(i2c_l, max98505);
  760. max98505->control_data = i2c_l;
  761. max98505->pdata = i2c_l->dev.platform_data;
  762. max98505->regmap = regmap_init_i2c(i2c_l, &max98505_regmap);
  763. if (IS_ERR(max98505->regmap)) {
  764. ret = PTR_ERR(max98505->regmap);
  765. dev_err(&i2c_l->dev, "Failed to allocate regmap: %d\n", ret);
  766. goto err_out;
  767. }
  768. ret = snd_soc_register_codec(&i2c_l->dev, &soc_codec_dev_max98505,
  769. max98505_dai, ARRAY_SIZE(max98505_dai));
  770. err_out:
  771. if (ret < 0) {
  772. if (max98505->regmap)
  773. regmap_exit(max98505->regmap);
  774. kfree(max98505);
  775. }
  776. dsm_misc_device_init();
  777. pr_info("%s: ret %d\n", __func__, ret);
  778. return ret;
  779. }
  780. static int max98505_i2c_remove(struct i2c_client *client)
  781. {
  782. struct max98505_priv *max98505 = dev_get_drvdata(&client->dev);
  783. snd_soc_unregister_codec(&client->dev);
  784. if (max98505->regmap)
  785. regmap_exit(max98505->regmap);
  786. kfree(i2c_get_clientdata(client));
  787. pr_info("%s: exit\n", __func__);
  788. dsm_misc_device_deinit();
  789. return 0;
  790. }
  791. static const struct i2c_device_id max98505_i2c_id[] = {
  792. { "max98505", MAX98505 },
  793. { }
  794. };
  795. MODULE_DEVICE_TABLE(i2c, max98505_i2c_id);
  796. static struct i2c_driver max98505_i2c_driver = {
  797. .driver = {
  798. .name = "max98505",
  799. .owner = THIS_MODULE,
  800. },
  801. .probe = max98505_i2c_probe,
  802. .remove = max98505_i2c_remove,
  803. .id_table = max98505_i2c_id,
  804. };
  805. module_i2c_driver(max98505_i2c_driver);
  806. MODULE_DESCRIPTION("ALSA SoC MAX98505 driver");
  807. MODULE_AUTHOR("Ralph Birt <rdbirt@gmail.com>");
  808. MODULE_LICENSE("GPL");