ssm2602.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. /*
  2. * File: sound/soc/codecs/ssm2602.c
  3. * Author: Cliff Cai <Cliff.Cai@analog.com>
  4. *
  5. * Created: Tue June 06 2008
  6. * Description: Driver for ssm2602 sound chip
  7. *
  8. * Modified:
  9. * Copyright 2008 Analog Devices Inc.
  10. *
  11. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, see the file COPYING, or write
  25. * to the Free Software Foundation, Inc.,
  26. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  27. */
  28. #include <linux/module.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/init.h>
  31. #include <linux/delay.h>
  32. #include <linux/pm.h>
  33. #include <linux/i2c.h>
  34. #include <linux/spi/spi.h>
  35. #include <linux/slab.h>
  36. #include <sound/core.h>
  37. #include <sound/pcm.h>
  38. #include <sound/pcm_params.h>
  39. #include <sound/soc.h>
  40. #include <sound/initval.h>
  41. #include <sound/tlv.h>
  42. #include "ssm2602.h"
  43. #define SSM2602_VERSION "0.1"
  44. enum ssm2602_type {
  45. SSM2602,
  46. SSM2604,
  47. };
  48. /* codec private data */
  49. struct ssm2602_priv {
  50. unsigned int sysclk;
  51. enum snd_soc_control_type control_type;
  52. struct snd_pcm_substream *master_substream;
  53. struct snd_pcm_substream *slave_substream;
  54. enum ssm2602_type type;
  55. unsigned int clk_out_pwr;
  56. };
  57. /*
  58. * ssm2602 register cache
  59. * We can't read the ssm2602 register space when we are
  60. * using 2 wire for device control, so we cache them instead.
  61. * There is no point in caching the reset register
  62. */
  63. static const u16 ssm2602_reg[SSM2602_CACHEREGNUM] = {
  64. 0x0097, 0x0097, 0x0079, 0x0079,
  65. 0x000a, 0x0008, 0x009f, 0x000a,
  66. 0x0000, 0x0000
  67. };
  68. #define ssm2602_reset(c) snd_soc_write(c, SSM2602_RESET, 0)
  69. /*Appending several "None"s just for OSS mixer use*/
  70. static const char *ssm2602_input_select[] = {
  71. "Line", "Mic", "None", "None", "None",
  72. "None", "None", "None",
  73. };
  74. static const char *ssm2602_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
  75. static const struct soc_enum ssm2602_enum[] = {
  76. SOC_ENUM_SINGLE(SSM2602_APANA, 2, 2, ssm2602_input_select),
  77. SOC_ENUM_SINGLE(SSM2602_APDIGI, 1, 4, ssm2602_deemph),
  78. };
  79. static const unsigned int ssm260x_outmix_tlv[] = {
  80. TLV_DB_RANGE_HEAD(2),
  81. 0, 47, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 0),
  82. 48, 127, TLV_DB_SCALE_ITEM(-7400, 100, 0),
  83. };
  84. static const DECLARE_TLV_DB_SCALE(ssm260x_inpga_tlv, -3450, 150, 0);
  85. static const DECLARE_TLV_DB_SCALE(ssm260x_sidetone_tlv, -1500, 300, 0);
  86. static const struct snd_kcontrol_new ssm260x_snd_controls[] = {
  87. SOC_DOUBLE_R_TLV("Capture Volume", SSM2602_LINVOL, SSM2602_RINVOL, 0, 45, 0,
  88. ssm260x_inpga_tlv),
  89. SOC_DOUBLE_R("Capture Switch", SSM2602_LINVOL, SSM2602_RINVOL, 7, 1, 1),
  90. SOC_SINGLE("ADC High Pass Filter Switch", SSM2602_APDIGI, 0, 1, 1),
  91. SOC_SINGLE("Store DC Offset Switch", SSM2602_APDIGI, 4, 1, 0),
  92. SOC_ENUM("Playback De-emphasis", ssm2602_enum[1]),
  93. };
  94. static const struct snd_kcontrol_new ssm2602_snd_controls[] = {
  95. SOC_DOUBLE_R_TLV("Master Playback Volume", SSM2602_LOUT1V, SSM2602_ROUT1V,
  96. 0, 127, 0, ssm260x_outmix_tlv),
  97. SOC_DOUBLE_R("Master Playback ZC Switch", SSM2602_LOUT1V, SSM2602_ROUT1V,
  98. 7, 1, 0),
  99. SOC_SINGLE_TLV("Sidetone Playback Volume", SSM2602_APANA, 6, 3, 1,
  100. ssm260x_sidetone_tlv),
  101. SOC_SINGLE("Mic Boost (+20dB)", SSM2602_APANA, 0, 1, 0),
  102. SOC_SINGLE("Mic Boost2 (+20dB)", SSM2602_APANA, 8, 1, 0),
  103. SOC_SINGLE("Mic Switch", SSM2602_APANA, 1, 1, 1),
  104. };
  105. /* Output Mixer */
  106. static const struct snd_kcontrol_new ssm260x_output_mixer_controls[] = {
  107. SOC_DAPM_SINGLE("Line Bypass Switch", SSM2602_APANA, 3, 1, 0),
  108. SOC_DAPM_SINGLE("HiFi Playback Switch", SSM2602_APANA, 4, 1, 0),
  109. SOC_DAPM_SINGLE("Mic Sidetone Switch", SSM2602_APANA, 5, 1, 0),
  110. };
  111. /* Input mux */
  112. static const struct snd_kcontrol_new ssm2602_input_mux_controls =
  113. SOC_DAPM_ENUM("Input Select", ssm2602_enum[0]);
  114. static const struct snd_soc_dapm_widget ssm260x_dapm_widgets[] = {
  115. SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SSM2602_PWR, 3, 1),
  116. SND_SOC_DAPM_ADC("ADC", "HiFi Capture", SSM2602_PWR, 2, 1),
  117. SND_SOC_DAPM_PGA("Line Input", SSM2602_PWR, 0, 1, NULL, 0),
  118. SND_SOC_DAPM_SUPPLY("Digital Core Power", SSM2602_ACTIVE, 0, 0, NULL, 0),
  119. SND_SOC_DAPM_OUTPUT("LOUT"),
  120. SND_SOC_DAPM_OUTPUT("ROUT"),
  121. SND_SOC_DAPM_INPUT("RLINEIN"),
  122. SND_SOC_DAPM_INPUT("LLINEIN"),
  123. };
  124. static const struct snd_soc_dapm_widget ssm2602_dapm_widgets[] = {
  125. SND_SOC_DAPM_MIXER("Output Mixer", SSM2602_PWR, 4, 1,
  126. ssm260x_output_mixer_controls,
  127. ARRAY_SIZE(ssm260x_output_mixer_controls)),
  128. SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &ssm2602_input_mux_controls),
  129. SND_SOC_DAPM_MICBIAS("Mic Bias", SSM2602_PWR, 1, 1),
  130. SND_SOC_DAPM_OUTPUT("LHPOUT"),
  131. SND_SOC_DAPM_OUTPUT("RHPOUT"),
  132. SND_SOC_DAPM_INPUT("MICIN"),
  133. };
  134. static const struct snd_soc_dapm_widget ssm2604_dapm_widgets[] = {
  135. SND_SOC_DAPM_MIXER("Output Mixer", SND_SOC_NOPM, 0, 0,
  136. ssm260x_output_mixer_controls,
  137. ARRAY_SIZE(ssm260x_output_mixer_controls) - 1), /* Last element is the mic */
  138. };
  139. static const struct snd_soc_dapm_route ssm260x_routes[] = {
  140. {"DAC", NULL, "Digital Core Power"},
  141. {"ADC", NULL, "Digital Core Power"},
  142. {"Output Mixer", "Line Bypass Switch", "Line Input"},
  143. {"Output Mixer", "HiFi Playback Switch", "DAC"},
  144. {"ROUT", NULL, "Output Mixer"},
  145. {"LOUT", NULL, "Output Mixer"},
  146. {"Line Input", NULL, "LLINEIN"},
  147. {"Line Input", NULL, "RLINEIN"},
  148. };
  149. static const struct snd_soc_dapm_route ssm2602_routes[] = {
  150. {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
  151. {"RHPOUT", NULL, "Output Mixer"},
  152. {"LHPOUT", NULL, "Output Mixer"},
  153. {"Input Mux", "Line", "Line Input"},
  154. {"Input Mux", "Mic", "Mic Bias"},
  155. {"ADC", NULL, "Input Mux"},
  156. {"Mic Bias", NULL, "MICIN"},
  157. };
  158. static const struct snd_soc_dapm_route ssm2604_routes[] = {
  159. {"ADC", NULL, "Line Input"},
  160. };
  161. struct ssm2602_coeff {
  162. u32 mclk;
  163. u32 rate;
  164. u8 srate;
  165. };
  166. #define SSM2602_COEFF_SRATE(sr, bosr, usb) (((sr) << 2) | ((bosr) << 1) | (usb))
  167. /* codec mclk clock coefficients */
  168. static const struct ssm2602_coeff ssm2602_coeff_table[] = {
  169. /* 48k */
  170. {12288000, 48000, SSM2602_COEFF_SRATE(0x0, 0x0, 0x0)},
  171. {18432000, 48000, SSM2602_COEFF_SRATE(0x0, 0x1, 0x0)},
  172. {12000000, 48000, SSM2602_COEFF_SRATE(0x0, 0x0, 0x1)},
  173. /* 32k */
  174. {12288000, 32000, SSM2602_COEFF_SRATE(0x6, 0x0, 0x0)},
  175. {18432000, 32000, SSM2602_COEFF_SRATE(0x6, 0x1, 0x0)},
  176. {12000000, 32000, SSM2602_COEFF_SRATE(0x6, 0x0, 0x1)},
  177. /* 8k */
  178. {12288000, 8000, SSM2602_COEFF_SRATE(0x3, 0x0, 0x0)},
  179. {18432000, 8000, SSM2602_COEFF_SRATE(0x3, 0x1, 0x0)},
  180. {11289600, 8000, SSM2602_COEFF_SRATE(0xb, 0x0, 0x0)},
  181. {16934400, 8000, SSM2602_COEFF_SRATE(0xb, 0x1, 0x0)},
  182. {12000000, 8000, SSM2602_COEFF_SRATE(0x3, 0x0, 0x1)},
  183. /* 96k */
  184. {12288000, 96000, SSM2602_COEFF_SRATE(0x7, 0x0, 0x0)},
  185. {18432000, 96000, SSM2602_COEFF_SRATE(0x7, 0x1, 0x0)},
  186. {12000000, 96000, SSM2602_COEFF_SRATE(0x7, 0x0, 0x1)},
  187. /* 44.1k */
  188. {11289600, 44100, SSM2602_COEFF_SRATE(0x8, 0x0, 0x0)},
  189. {16934400, 44100, SSM2602_COEFF_SRATE(0x8, 0x1, 0x0)},
  190. {12000000, 44100, SSM2602_COEFF_SRATE(0x8, 0x1, 0x1)},
  191. /* 88.2k */
  192. {11289600, 88200, SSM2602_COEFF_SRATE(0xf, 0x0, 0x0)},
  193. {16934400, 88200, SSM2602_COEFF_SRATE(0xf, 0x1, 0x0)},
  194. {12000000, 88200, SSM2602_COEFF_SRATE(0xf, 0x1, 0x1)},
  195. };
  196. static inline int ssm2602_get_coeff(int mclk, int rate)
  197. {
  198. int i;
  199. for (i = 0; i < ARRAY_SIZE(ssm2602_coeff_table); i++) {
  200. if (ssm2602_coeff_table[i].rate == rate &&
  201. ssm2602_coeff_table[i].mclk == mclk)
  202. return ssm2602_coeff_table[i].srate;
  203. }
  204. return -EINVAL;
  205. }
  206. static int ssm2602_hw_params(struct snd_pcm_substream *substream,
  207. struct snd_pcm_hw_params *params,
  208. struct snd_soc_dai *dai)
  209. {
  210. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  211. struct snd_soc_codec *codec = rtd->codec;
  212. struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
  213. u16 iface = snd_soc_read(codec, SSM2602_IFACE) & 0xfff3;
  214. int srate = ssm2602_get_coeff(ssm2602->sysclk, params_rate(params));
  215. if (substream == ssm2602->slave_substream) {
  216. dev_dbg(codec->dev, "Ignoring hw_params for slave substream\n");
  217. return 0;
  218. }
  219. if (srate < 0)
  220. return srate;
  221. snd_soc_write(codec, SSM2602_SRATE, srate);
  222. /* bit size */
  223. switch (params_format(params)) {
  224. case SNDRV_PCM_FORMAT_S16_LE:
  225. break;
  226. case SNDRV_PCM_FORMAT_S20_3LE:
  227. iface |= 0x0004;
  228. break;
  229. case SNDRV_PCM_FORMAT_S24_LE:
  230. iface |= 0x0008;
  231. break;
  232. case SNDRV_PCM_FORMAT_S32_LE:
  233. iface |= 0x000c;
  234. break;
  235. }
  236. snd_soc_write(codec, SSM2602_IFACE, iface);
  237. return 0;
  238. }
  239. static int ssm2602_startup(struct snd_pcm_substream *substream,
  240. struct snd_soc_dai *dai)
  241. {
  242. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  243. struct snd_soc_codec *codec = rtd->codec;
  244. struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
  245. struct snd_pcm_runtime *master_runtime;
  246. /* The DAI has shared clocks so if we already have a playback or
  247. * capture going then constrain this substream to match it.
  248. * TODO: the ssm2602 allows pairs of non-matching PB/REC rates
  249. */
  250. if (ssm2602->master_substream) {
  251. master_runtime = ssm2602->master_substream->runtime;
  252. dev_dbg(codec->dev, "Constraining to %d bits at %dHz\n",
  253. master_runtime->sample_bits,
  254. master_runtime->rate);
  255. if (master_runtime->rate != 0)
  256. snd_pcm_hw_constraint_minmax(substream->runtime,
  257. SNDRV_PCM_HW_PARAM_RATE,
  258. master_runtime->rate,
  259. master_runtime->rate);
  260. if (master_runtime->sample_bits != 0)
  261. snd_pcm_hw_constraint_minmax(substream->runtime,
  262. SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
  263. master_runtime->sample_bits,
  264. master_runtime->sample_bits);
  265. ssm2602->slave_substream = substream;
  266. } else
  267. ssm2602->master_substream = substream;
  268. return 0;
  269. }
  270. static void ssm2602_shutdown(struct snd_pcm_substream *substream,
  271. struct snd_soc_dai *dai)
  272. {
  273. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  274. struct snd_soc_codec *codec = rtd->codec;
  275. struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
  276. if (ssm2602->master_substream == substream)
  277. ssm2602->master_substream = ssm2602->slave_substream;
  278. ssm2602->slave_substream = NULL;
  279. }
  280. static int ssm2602_mute(struct snd_soc_dai *dai, int mute)
  281. {
  282. struct snd_soc_codec *codec = dai->codec;
  283. if (mute)
  284. snd_soc_update_bits(codec, SSM2602_APDIGI,
  285. APDIGI_ENABLE_DAC_MUTE,
  286. APDIGI_ENABLE_DAC_MUTE);
  287. else
  288. snd_soc_update_bits(codec, SSM2602_APDIGI,
  289. APDIGI_ENABLE_DAC_MUTE, 0);
  290. return 0;
  291. }
  292. static int ssm2602_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  293. int clk_id, unsigned int freq, int dir)
  294. {
  295. struct snd_soc_codec *codec = codec_dai->codec;
  296. struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
  297. if (dir == SND_SOC_CLOCK_IN) {
  298. if (clk_id != SSM2602_SYSCLK)
  299. return -EINVAL;
  300. switch (freq) {
  301. case 11289600:
  302. case 12000000:
  303. case 12288000:
  304. case 16934400:
  305. case 18432000:
  306. ssm2602->sysclk = freq;
  307. break;
  308. default:
  309. return -EINVAL;
  310. }
  311. } else {
  312. unsigned int mask;
  313. switch (clk_id) {
  314. case SSM2602_CLK_CLKOUT:
  315. mask = PWR_CLK_OUT_PDN;
  316. break;
  317. case SSM2602_CLK_XTO:
  318. mask = PWR_OSC_PDN;
  319. break;
  320. default:
  321. return -EINVAL;
  322. }
  323. if (freq == 0)
  324. ssm2602->clk_out_pwr |= mask;
  325. else
  326. ssm2602->clk_out_pwr &= ~mask;
  327. snd_soc_update_bits(codec, SSM2602_PWR,
  328. PWR_CLK_OUT_PDN | PWR_OSC_PDN, ssm2602->clk_out_pwr);
  329. }
  330. return 0;
  331. }
  332. static int ssm2602_set_dai_fmt(struct snd_soc_dai *codec_dai,
  333. unsigned int fmt)
  334. {
  335. struct snd_soc_codec *codec = codec_dai->codec;
  336. u16 iface = 0;
  337. /* set master/slave audio interface */
  338. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  339. case SND_SOC_DAIFMT_CBM_CFM:
  340. iface |= 0x0040;
  341. break;
  342. case SND_SOC_DAIFMT_CBS_CFS:
  343. break;
  344. default:
  345. return -EINVAL;
  346. }
  347. /* interface format */
  348. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  349. case SND_SOC_DAIFMT_I2S:
  350. iface |= 0x0002;
  351. break;
  352. case SND_SOC_DAIFMT_RIGHT_J:
  353. break;
  354. case SND_SOC_DAIFMT_LEFT_J:
  355. iface |= 0x0001;
  356. break;
  357. case SND_SOC_DAIFMT_DSP_A:
  358. iface |= 0x0013;
  359. break;
  360. case SND_SOC_DAIFMT_DSP_B:
  361. iface |= 0x0003;
  362. break;
  363. default:
  364. return -EINVAL;
  365. }
  366. /* clock inversion */
  367. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  368. case SND_SOC_DAIFMT_NB_NF:
  369. break;
  370. case SND_SOC_DAIFMT_IB_IF:
  371. iface |= 0x0090;
  372. break;
  373. case SND_SOC_DAIFMT_IB_NF:
  374. iface |= 0x0080;
  375. break;
  376. case SND_SOC_DAIFMT_NB_IF:
  377. iface |= 0x0010;
  378. break;
  379. default:
  380. return -EINVAL;
  381. }
  382. /* set iface */
  383. snd_soc_write(codec, SSM2602_IFACE, iface);
  384. return 0;
  385. }
  386. static int ssm2602_set_bias_level(struct snd_soc_codec *codec,
  387. enum snd_soc_bias_level level)
  388. {
  389. struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
  390. switch (level) {
  391. case SND_SOC_BIAS_ON:
  392. /* vref/mid on, osc and clkout on if enabled */
  393. snd_soc_update_bits(codec, SSM2602_PWR,
  394. PWR_POWER_OFF | PWR_CLK_OUT_PDN | PWR_OSC_PDN,
  395. ssm2602->clk_out_pwr);
  396. break;
  397. case SND_SOC_BIAS_PREPARE:
  398. break;
  399. case SND_SOC_BIAS_STANDBY:
  400. /* everything off except vref/vmid, */
  401. snd_soc_update_bits(codec, SSM2602_PWR,
  402. PWR_POWER_OFF | PWR_CLK_OUT_PDN | PWR_OSC_PDN,
  403. PWR_CLK_OUT_PDN | PWR_OSC_PDN);
  404. break;
  405. case SND_SOC_BIAS_OFF:
  406. /* everything off */
  407. snd_soc_update_bits(codec, SSM2602_PWR,
  408. PWR_POWER_OFF, PWR_POWER_OFF);
  409. break;
  410. }
  411. codec->dapm.bias_level = level;
  412. return 0;
  413. }
  414. #define SSM2602_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_32000 |\
  415. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
  416. SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
  417. #define SSM2602_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  418. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  419. static const struct snd_soc_dai_ops ssm2602_dai_ops = {
  420. .startup = ssm2602_startup,
  421. .hw_params = ssm2602_hw_params,
  422. .shutdown = ssm2602_shutdown,
  423. .digital_mute = ssm2602_mute,
  424. .set_sysclk = ssm2602_set_dai_sysclk,
  425. .set_fmt = ssm2602_set_dai_fmt,
  426. };
  427. static struct snd_soc_dai_driver ssm2602_dai = {
  428. .name = "ssm2602-hifi",
  429. .playback = {
  430. .stream_name = "Playback",
  431. .channels_min = 2,
  432. .channels_max = 2,
  433. .rates = SSM2602_RATES,
  434. .formats = SSM2602_FORMATS,},
  435. .capture = {
  436. .stream_name = "Capture",
  437. .channels_min = 2,
  438. .channels_max = 2,
  439. .rates = SSM2602_RATES,
  440. .formats = SSM2602_FORMATS,},
  441. .ops = &ssm2602_dai_ops,
  442. };
  443. static int ssm2602_suspend(struct snd_soc_codec *codec)
  444. {
  445. ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
  446. return 0;
  447. }
  448. static int ssm2602_resume(struct snd_soc_codec *codec)
  449. {
  450. snd_soc_cache_sync(codec);
  451. ssm2602_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  452. return 0;
  453. }
  454. static int ssm2602_probe(struct snd_soc_codec *codec)
  455. {
  456. struct snd_soc_dapm_context *dapm = &codec->dapm;
  457. int ret;
  458. snd_soc_update_bits(codec, SSM2602_LOUT1V,
  459. LOUT1V_LRHP_BOTH, LOUT1V_LRHP_BOTH);
  460. snd_soc_update_bits(codec, SSM2602_ROUT1V,
  461. ROUT1V_RLHP_BOTH, ROUT1V_RLHP_BOTH);
  462. ret = snd_soc_add_codec_controls(codec, ssm2602_snd_controls,
  463. ARRAY_SIZE(ssm2602_snd_controls));
  464. if (ret)
  465. return ret;
  466. ret = snd_soc_dapm_new_controls(dapm, ssm2602_dapm_widgets,
  467. ARRAY_SIZE(ssm2602_dapm_widgets));
  468. if (ret)
  469. return ret;
  470. return snd_soc_dapm_add_routes(dapm, ssm2602_routes,
  471. ARRAY_SIZE(ssm2602_routes));
  472. }
  473. static int ssm2604_probe(struct snd_soc_codec *codec)
  474. {
  475. struct snd_soc_dapm_context *dapm = &codec->dapm;
  476. int ret;
  477. ret = snd_soc_dapm_new_controls(dapm, ssm2604_dapm_widgets,
  478. ARRAY_SIZE(ssm2604_dapm_widgets));
  479. if (ret)
  480. return ret;
  481. return snd_soc_dapm_add_routes(dapm, ssm2604_routes,
  482. ARRAY_SIZE(ssm2604_routes));
  483. }
  484. static int ssm260x_probe(struct snd_soc_codec *codec)
  485. {
  486. struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
  487. int ret;
  488. pr_info("ssm2602 Audio Codec %s", SSM2602_VERSION);
  489. ret = snd_soc_codec_set_cache_io(codec, 7, 9, ssm2602->control_type);
  490. if (ret < 0) {
  491. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  492. return ret;
  493. }
  494. ret = ssm2602_reset(codec);
  495. if (ret < 0) {
  496. dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
  497. return ret;
  498. }
  499. /* set the update bits */
  500. snd_soc_update_bits(codec, SSM2602_LINVOL,
  501. LINVOL_LRIN_BOTH, LINVOL_LRIN_BOTH);
  502. snd_soc_update_bits(codec, SSM2602_RINVOL,
  503. RINVOL_RLIN_BOTH, RINVOL_RLIN_BOTH);
  504. /*select Line in as default input*/
  505. snd_soc_write(codec, SSM2602_APANA, APANA_SELECT_DAC |
  506. APANA_ENABLE_MIC_BOOST);
  507. switch (ssm2602->type) {
  508. case SSM2602:
  509. ret = ssm2602_probe(codec);
  510. break;
  511. case SSM2604:
  512. ret = ssm2604_probe(codec);
  513. break;
  514. }
  515. if (ret)
  516. return ret;
  517. ssm2602_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  518. return 0;
  519. }
  520. /* remove everything here */
  521. static int ssm2602_remove(struct snd_soc_codec *codec)
  522. {
  523. ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
  524. return 0;
  525. }
  526. static struct snd_soc_codec_driver soc_codec_dev_ssm2602 = {
  527. .probe = ssm260x_probe,
  528. .remove = ssm2602_remove,
  529. .suspend = ssm2602_suspend,
  530. .resume = ssm2602_resume,
  531. .set_bias_level = ssm2602_set_bias_level,
  532. .reg_cache_size = ARRAY_SIZE(ssm2602_reg),
  533. .reg_word_size = sizeof(u16),
  534. .reg_cache_default = ssm2602_reg,
  535. .controls = ssm260x_snd_controls,
  536. .num_controls = ARRAY_SIZE(ssm260x_snd_controls),
  537. .dapm_widgets = ssm260x_dapm_widgets,
  538. .num_dapm_widgets = ARRAY_SIZE(ssm260x_dapm_widgets),
  539. .dapm_routes = ssm260x_routes,
  540. .num_dapm_routes = ARRAY_SIZE(ssm260x_routes),
  541. };
  542. #if defined(CONFIG_SPI_MASTER)
  543. static int __devinit ssm2602_spi_probe(struct spi_device *spi)
  544. {
  545. struct ssm2602_priv *ssm2602;
  546. int ret;
  547. ssm2602 = devm_kzalloc(&spi->dev, sizeof(struct ssm2602_priv),
  548. GFP_KERNEL);
  549. if (ssm2602 == NULL)
  550. return -ENOMEM;
  551. spi_set_drvdata(spi, ssm2602);
  552. ssm2602->control_type = SND_SOC_SPI;
  553. ssm2602->type = SSM2602;
  554. ret = snd_soc_register_codec(&spi->dev,
  555. &soc_codec_dev_ssm2602, &ssm2602_dai, 1);
  556. return ret;
  557. }
  558. static int __devexit ssm2602_spi_remove(struct spi_device *spi)
  559. {
  560. snd_soc_unregister_codec(&spi->dev);
  561. return 0;
  562. }
  563. static struct spi_driver ssm2602_spi_driver = {
  564. .driver = {
  565. .name = "ssm2602",
  566. .owner = THIS_MODULE,
  567. },
  568. .probe = ssm2602_spi_probe,
  569. .remove = __devexit_p(ssm2602_spi_remove),
  570. };
  571. #endif
  572. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  573. /*
  574. * ssm2602 2 wire address is determined by GPIO5
  575. * state during powerup.
  576. * low = 0x1a
  577. * high = 0x1b
  578. */
  579. static int __devinit ssm2602_i2c_probe(struct i2c_client *i2c,
  580. const struct i2c_device_id *id)
  581. {
  582. struct ssm2602_priv *ssm2602;
  583. int ret;
  584. ssm2602 = devm_kzalloc(&i2c->dev, sizeof(struct ssm2602_priv),
  585. GFP_KERNEL);
  586. if (ssm2602 == NULL)
  587. return -ENOMEM;
  588. i2c_set_clientdata(i2c, ssm2602);
  589. ssm2602->control_type = SND_SOC_I2C;
  590. ssm2602->type = id->driver_data;
  591. ret = snd_soc_register_codec(&i2c->dev,
  592. &soc_codec_dev_ssm2602, &ssm2602_dai, 1);
  593. return ret;
  594. }
  595. static int __devexit ssm2602_i2c_remove(struct i2c_client *client)
  596. {
  597. snd_soc_unregister_codec(&client->dev);
  598. return 0;
  599. }
  600. static const struct i2c_device_id ssm2602_i2c_id[] = {
  601. { "ssm2602", SSM2602 },
  602. { "ssm2603", SSM2602 },
  603. { "ssm2604", SSM2604 },
  604. { }
  605. };
  606. MODULE_DEVICE_TABLE(i2c, ssm2602_i2c_id);
  607. /* corgi i2c codec control layer */
  608. static struct i2c_driver ssm2602_i2c_driver = {
  609. .driver = {
  610. .name = "ssm2602",
  611. .owner = THIS_MODULE,
  612. },
  613. .probe = ssm2602_i2c_probe,
  614. .remove = __devexit_p(ssm2602_i2c_remove),
  615. .id_table = ssm2602_i2c_id,
  616. };
  617. #endif
  618. static int __init ssm2602_modinit(void)
  619. {
  620. int ret = 0;
  621. #if defined(CONFIG_SPI_MASTER)
  622. ret = spi_register_driver(&ssm2602_spi_driver);
  623. if (ret)
  624. return ret;
  625. #endif
  626. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  627. ret = i2c_add_driver(&ssm2602_i2c_driver);
  628. if (ret)
  629. return ret;
  630. #endif
  631. return ret;
  632. }
  633. module_init(ssm2602_modinit);
  634. static void __exit ssm2602_exit(void)
  635. {
  636. #if defined(CONFIG_SPI_MASTER)
  637. spi_unregister_driver(&ssm2602_spi_driver);
  638. #endif
  639. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  640. i2c_del_driver(&ssm2602_i2c_driver);
  641. #endif
  642. }
  643. module_exit(ssm2602_exit);
  644. MODULE_DESCRIPTION("ASoC SSM2602/SSM2603/SSM2604 driver");
  645. MODULE_AUTHOR("Cliff Cai");
  646. MODULE_LICENSE("GPL");