tas.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Apple Onboard Audio driver for tas codec
  4. *
  5. * Copyright 2006 Johannes Berg <[email protected]>
  6. *
  7. * Open questions:
  8. * - How to distinguish between 3004 and versions?
  9. *
  10. * FIXMEs:
  11. * - This codec driver doesn't honour the 'connected'
  12. * property of the aoa_codec struct, hence if
  13. * it is used in machines where not everything is
  14. * connected it will display wrong mixer elements.
  15. * - Driver assumes that the microphone is always
  16. * monaureal and connected to the right channel of
  17. * the input. This should also be a codec-dependent
  18. * flag, maybe the codec should have 3 different
  19. * bits for the three different possibilities how
  20. * it can be hooked up...
  21. * But as long as I don't see any hardware hooked
  22. * up that way...
  23. * - As Apple notes in their code, the tas3004 seems
  24. * to delay the right channel by one sample. You can
  25. * see this when for example recording stereo in
  26. * audacity, or recording the tas output via cable
  27. * on another machine (use a sinus generator or so).
  28. * I tried programming the BiQuads but couldn't
  29. * make the delay work, maybe someone can read the
  30. * datasheet and fix it. The relevant Apple comment
  31. * is in AppleTAS3004Audio.cpp lines 1637 ff. Note
  32. * that their comment describing how they program
  33. * the filters sucks...
  34. *
  35. * Other things:
  36. * - this should actually register *two* aoa_codec
  37. * structs since it has two inputs. Then it must
  38. * use the prepare callback to forbid running the
  39. * secondary output on a different clock.
  40. * Also, whatever bus knows how to do this must
  41. * provide two soundbus_dev devices and the fabric
  42. * must be able to link them correctly.
  43. *
  44. * I don't even know if Apple ever uses the second
  45. * port on the tas3004 though, I don't think their
  46. * i2s controllers can even do it. OTOH, they all
  47. * derive the clocks from common clocks, so it
  48. * might just be possible. The framework allows the
  49. * codec to refine the transfer_info items in the
  50. * usable callback, so we can simply remove the
  51. * rates the second instance is not using when it
  52. * actually is in use.
  53. * Maybe we'll need to make the sound busses have
  54. * a 'clock group id' value so the codec can
  55. * determine if the two outputs can be driven at
  56. * the same time. But that is likely overkill, up
  57. * to the fabric to not link them up incorrectly,
  58. * and up to the hardware designer to not wire
  59. * them up in some weird unusable way.
  60. */
  61. #include <linux/i2c.h>
  62. #include <asm/pmac_low_i2c.h>
  63. #include <asm/prom.h>
  64. #include <linux/delay.h>
  65. #include <linux/module.h>
  66. #include <linux/mutex.h>
  67. #include <linux/slab.h>
  68. MODULE_AUTHOR("Johannes Berg <[email protected]>");
  69. MODULE_LICENSE("GPL");
  70. MODULE_DESCRIPTION("tas codec driver for snd-aoa");
  71. #include "tas.h"
  72. #include "tas-gain-table.h"
  73. #include "tas-basstreble.h"
  74. #include "../aoa.h"
  75. #include "../soundbus/soundbus.h"
  76. #define PFX "snd-aoa-codec-tas: "
  77. struct tas {
  78. struct aoa_codec codec;
  79. struct i2c_client *i2c;
  80. u32 mute_l:1, mute_r:1 ,
  81. controls_created:1 ,
  82. drc_enabled:1,
  83. hw_enabled:1;
  84. u8 cached_volume_l, cached_volume_r;
  85. u8 mixer_l[3], mixer_r[3];
  86. u8 bass, treble;
  87. u8 acr;
  88. int drc_range;
  89. /* protects hardware access against concurrency from
  90. * userspace when hitting controls and during
  91. * codec init/suspend/resume */
  92. struct mutex mtx;
  93. };
  94. static int tas_reset_init(struct tas *tas);
  95. static struct tas *codec_to_tas(struct aoa_codec *codec)
  96. {
  97. return container_of(codec, struct tas, codec);
  98. }
  99. static inline int tas_write_reg(struct tas *tas, u8 reg, u8 len, u8 *data)
  100. {
  101. if (len == 1)
  102. return i2c_smbus_write_byte_data(tas->i2c, reg, *data);
  103. else
  104. return i2c_smbus_write_i2c_block_data(tas->i2c, reg, len, data);
  105. }
  106. static void tas3004_set_drc(struct tas *tas)
  107. {
  108. unsigned char val[6];
  109. if (tas->drc_enabled)
  110. val[0] = 0x50; /* 3:1 above threshold */
  111. else
  112. val[0] = 0x51; /* disabled */
  113. val[1] = 0x02; /* 1:1 below threshold */
  114. if (tas->drc_range > 0xef)
  115. val[2] = 0xef;
  116. else if (tas->drc_range < 0)
  117. val[2] = 0x00;
  118. else
  119. val[2] = tas->drc_range;
  120. val[3] = 0xb0;
  121. val[4] = 0x60;
  122. val[5] = 0xa0;
  123. tas_write_reg(tas, TAS_REG_DRC, 6, val);
  124. }
  125. static void tas_set_treble(struct tas *tas)
  126. {
  127. u8 tmp;
  128. tmp = tas3004_treble(tas->treble);
  129. tas_write_reg(tas, TAS_REG_TREBLE, 1, &tmp);
  130. }
  131. static void tas_set_bass(struct tas *tas)
  132. {
  133. u8 tmp;
  134. tmp = tas3004_bass(tas->bass);
  135. tas_write_reg(tas, TAS_REG_BASS, 1, &tmp);
  136. }
  137. static void tas_set_volume(struct tas *tas)
  138. {
  139. u8 block[6];
  140. int tmp;
  141. u8 left, right;
  142. left = tas->cached_volume_l;
  143. right = tas->cached_volume_r;
  144. if (left > 177) left = 177;
  145. if (right > 177) right = 177;
  146. if (tas->mute_l) left = 0;
  147. if (tas->mute_r) right = 0;
  148. /* analysing the volume and mixer tables shows
  149. * that they are similar enough when we shift
  150. * the mixer table down by 4 bits. The error
  151. * is miniscule, in just one item the error
  152. * is 1, at a value of 0x07f17b (mixer table
  153. * value is 0x07f17a) */
  154. tmp = tas_gaintable[left];
  155. block[0] = tmp>>20;
  156. block[1] = tmp>>12;
  157. block[2] = tmp>>4;
  158. tmp = tas_gaintable[right];
  159. block[3] = tmp>>20;
  160. block[4] = tmp>>12;
  161. block[5] = tmp>>4;
  162. tas_write_reg(tas, TAS_REG_VOL, 6, block);
  163. }
  164. static void tas_set_mixer(struct tas *tas)
  165. {
  166. u8 block[9];
  167. int tmp, i;
  168. u8 val;
  169. for (i=0;i<3;i++) {
  170. val = tas->mixer_l[i];
  171. if (val > 177) val = 177;
  172. tmp = tas_gaintable[val];
  173. block[3*i+0] = tmp>>16;
  174. block[3*i+1] = tmp>>8;
  175. block[3*i+2] = tmp;
  176. }
  177. tas_write_reg(tas, TAS_REG_LMIX, 9, block);
  178. for (i=0;i<3;i++) {
  179. val = tas->mixer_r[i];
  180. if (val > 177) val = 177;
  181. tmp = tas_gaintable[val];
  182. block[3*i+0] = tmp>>16;
  183. block[3*i+1] = tmp>>8;
  184. block[3*i+2] = tmp;
  185. }
  186. tas_write_reg(tas, TAS_REG_RMIX, 9, block);
  187. }
  188. /* alsa stuff */
  189. static int tas_dev_register(struct snd_device *dev)
  190. {
  191. return 0;
  192. }
  193. static const struct snd_device_ops ops = {
  194. .dev_register = tas_dev_register,
  195. };
  196. static int tas_snd_vol_info(struct snd_kcontrol *kcontrol,
  197. struct snd_ctl_elem_info *uinfo)
  198. {
  199. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  200. uinfo->count = 2;
  201. uinfo->value.integer.min = 0;
  202. uinfo->value.integer.max = 177;
  203. return 0;
  204. }
  205. static int tas_snd_vol_get(struct snd_kcontrol *kcontrol,
  206. struct snd_ctl_elem_value *ucontrol)
  207. {
  208. struct tas *tas = snd_kcontrol_chip(kcontrol);
  209. mutex_lock(&tas->mtx);
  210. ucontrol->value.integer.value[0] = tas->cached_volume_l;
  211. ucontrol->value.integer.value[1] = tas->cached_volume_r;
  212. mutex_unlock(&tas->mtx);
  213. return 0;
  214. }
  215. static int tas_snd_vol_put(struct snd_kcontrol *kcontrol,
  216. struct snd_ctl_elem_value *ucontrol)
  217. {
  218. struct tas *tas = snd_kcontrol_chip(kcontrol);
  219. if (ucontrol->value.integer.value[0] < 0 ||
  220. ucontrol->value.integer.value[0] > 177)
  221. return -EINVAL;
  222. if (ucontrol->value.integer.value[1] < 0 ||
  223. ucontrol->value.integer.value[1] > 177)
  224. return -EINVAL;
  225. mutex_lock(&tas->mtx);
  226. if (tas->cached_volume_l == ucontrol->value.integer.value[0]
  227. && tas->cached_volume_r == ucontrol->value.integer.value[1]) {
  228. mutex_unlock(&tas->mtx);
  229. return 0;
  230. }
  231. tas->cached_volume_l = ucontrol->value.integer.value[0];
  232. tas->cached_volume_r = ucontrol->value.integer.value[1];
  233. if (tas->hw_enabled)
  234. tas_set_volume(tas);
  235. mutex_unlock(&tas->mtx);
  236. return 1;
  237. }
  238. static const struct snd_kcontrol_new volume_control = {
  239. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  240. .name = "Master Playback Volume",
  241. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  242. .info = tas_snd_vol_info,
  243. .get = tas_snd_vol_get,
  244. .put = tas_snd_vol_put,
  245. };
  246. #define tas_snd_mute_info snd_ctl_boolean_stereo_info
  247. static int tas_snd_mute_get(struct snd_kcontrol *kcontrol,
  248. struct snd_ctl_elem_value *ucontrol)
  249. {
  250. struct tas *tas = snd_kcontrol_chip(kcontrol);
  251. mutex_lock(&tas->mtx);
  252. ucontrol->value.integer.value[0] = !tas->mute_l;
  253. ucontrol->value.integer.value[1] = !tas->mute_r;
  254. mutex_unlock(&tas->mtx);
  255. return 0;
  256. }
  257. static int tas_snd_mute_put(struct snd_kcontrol *kcontrol,
  258. struct snd_ctl_elem_value *ucontrol)
  259. {
  260. struct tas *tas = snd_kcontrol_chip(kcontrol);
  261. mutex_lock(&tas->mtx);
  262. if (tas->mute_l == !ucontrol->value.integer.value[0]
  263. && tas->mute_r == !ucontrol->value.integer.value[1]) {
  264. mutex_unlock(&tas->mtx);
  265. return 0;
  266. }
  267. tas->mute_l = !ucontrol->value.integer.value[0];
  268. tas->mute_r = !ucontrol->value.integer.value[1];
  269. if (tas->hw_enabled)
  270. tas_set_volume(tas);
  271. mutex_unlock(&tas->mtx);
  272. return 1;
  273. }
  274. static const struct snd_kcontrol_new mute_control = {
  275. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  276. .name = "Master Playback Switch",
  277. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  278. .info = tas_snd_mute_info,
  279. .get = tas_snd_mute_get,
  280. .put = tas_snd_mute_put,
  281. };
  282. static int tas_snd_mixer_info(struct snd_kcontrol *kcontrol,
  283. struct snd_ctl_elem_info *uinfo)
  284. {
  285. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  286. uinfo->count = 2;
  287. uinfo->value.integer.min = 0;
  288. uinfo->value.integer.max = 177;
  289. return 0;
  290. }
  291. static int tas_snd_mixer_get(struct snd_kcontrol *kcontrol,
  292. struct snd_ctl_elem_value *ucontrol)
  293. {
  294. struct tas *tas = snd_kcontrol_chip(kcontrol);
  295. int idx = kcontrol->private_value;
  296. mutex_lock(&tas->mtx);
  297. ucontrol->value.integer.value[0] = tas->mixer_l[idx];
  298. ucontrol->value.integer.value[1] = tas->mixer_r[idx];
  299. mutex_unlock(&tas->mtx);
  300. return 0;
  301. }
  302. static int tas_snd_mixer_put(struct snd_kcontrol *kcontrol,
  303. struct snd_ctl_elem_value *ucontrol)
  304. {
  305. struct tas *tas = snd_kcontrol_chip(kcontrol);
  306. int idx = kcontrol->private_value;
  307. mutex_lock(&tas->mtx);
  308. if (tas->mixer_l[idx] == ucontrol->value.integer.value[0]
  309. && tas->mixer_r[idx] == ucontrol->value.integer.value[1]) {
  310. mutex_unlock(&tas->mtx);
  311. return 0;
  312. }
  313. tas->mixer_l[idx] = ucontrol->value.integer.value[0];
  314. tas->mixer_r[idx] = ucontrol->value.integer.value[1];
  315. if (tas->hw_enabled)
  316. tas_set_mixer(tas);
  317. mutex_unlock(&tas->mtx);
  318. return 1;
  319. }
  320. #define MIXER_CONTROL(n,descr,idx) \
  321. static const struct snd_kcontrol_new n##_control = { \
  322. .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  323. .name = descr " Playback Volume", \
  324. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
  325. .info = tas_snd_mixer_info, \
  326. .get = tas_snd_mixer_get, \
  327. .put = tas_snd_mixer_put, \
  328. .private_value = idx, \
  329. }
  330. MIXER_CONTROL(pcm1, "PCM", 0);
  331. MIXER_CONTROL(monitor, "Monitor", 2);
  332. static int tas_snd_drc_range_info(struct snd_kcontrol *kcontrol,
  333. struct snd_ctl_elem_info *uinfo)
  334. {
  335. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  336. uinfo->count = 1;
  337. uinfo->value.integer.min = 0;
  338. uinfo->value.integer.max = TAS3004_DRC_MAX;
  339. return 0;
  340. }
  341. static int tas_snd_drc_range_get(struct snd_kcontrol *kcontrol,
  342. struct snd_ctl_elem_value *ucontrol)
  343. {
  344. struct tas *tas = snd_kcontrol_chip(kcontrol);
  345. mutex_lock(&tas->mtx);
  346. ucontrol->value.integer.value[0] = tas->drc_range;
  347. mutex_unlock(&tas->mtx);
  348. return 0;
  349. }
  350. static int tas_snd_drc_range_put(struct snd_kcontrol *kcontrol,
  351. struct snd_ctl_elem_value *ucontrol)
  352. {
  353. struct tas *tas = snd_kcontrol_chip(kcontrol);
  354. if (ucontrol->value.integer.value[0] < 0 ||
  355. ucontrol->value.integer.value[0] > TAS3004_DRC_MAX)
  356. return -EINVAL;
  357. mutex_lock(&tas->mtx);
  358. if (tas->drc_range == ucontrol->value.integer.value[0]) {
  359. mutex_unlock(&tas->mtx);
  360. return 0;
  361. }
  362. tas->drc_range = ucontrol->value.integer.value[0];
  363. if (tas->hw_enabled)
  364. tas3004_set_drc(tas);
  365. mutex_unlock(&tas->mtx);
  366. return 1;
  367. }
  368. static const struct snd_kcontrol_new drc_range_control = {
  369. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  370. .name = "DRC Range",
  371. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  372. .info = tas_snd_drc_range_info,
  373. .get = tas_snd_drc_range_get,
  374. .put = tas_snd_drc_range_put,
  375. };
  376. #define tas_snd_drc_switch_info snd_ctl_boolean_mono_info
  377. static int tas_snd_drc_switch_get(struct snd_kcontrol *kcontrol,
  378. struct snd_ctl_elem_value *ucontrol)
  379. {
  380. struct tas *tas = snd_kcontrol_chip(kcontrol);
  381. mutex_lock(&tas->mtx);
  382. ucontrol->value.integer.value[0] = tas->drc_enabled;
  383. mutex_unlock(&tas->mtx);
  384. return 0;
  385. }
  386. static int tas_snd_drc_switch_put(struct snd_kcontrol *kcontrol,
  387. struct snd_ctl_elem_value *ucontrol)
  388. {
  389. struct tas *tas = snd_kcontrol_chip(kcontrol);
  390. mutex_lock(&tas->mtx);
  391. if (tas->drc_enabled == ucontrol->value.integer.value[0]) {
  392. mutex_unlock(&tas->mtx);
  393. return 0;
  394. }
  395. tas->drc_enabled = !!ucontrol->value.integer.value[0];
  396. if (tas->hw_enabled)
  397. tas3004_set_drc(tas);
  398. mutex_unlock(&tas->mtx);
  399. return 1;
  400. }
  401. static const struct snd_kcontrol_new drc_switch_control = {
  402. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  403. .name = "DRC Range Switch",
  404. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  405. .info = tas_snd_drc_switch_info,
  406. .get = tas_snd_drc_switch_get,
  407. .put = tas_snd_drc_switch_put,
  408. };
  409. static int tas_snd_capture_source_info(struct snd_kcontrol *kcontrol,
  410. struct snd_ctl_elem_info *uinfo)
  411. {
  412. static const char * const texts[] = { "Line-In", "Microphone" };
  413. return snd_ctl_enum_info(uinfo, 1, 2, texts);
  414. }
  415. static int tas_snd_capture_source_get(struct snd_kcontrol *kcontrol,
  416. struct snd_ctl_elem_value *ucontrol)
  417. {
  418. struct tas *tas = snd_kcontrol_chip(kcontrol);
  419. mutex_lock(&tas->mtx);
  420. ucontrol->value.enumerated.item[0] = !!(tas->acr & TAS_ACR_INPUT_B);
  421. mutex_unlock(&tas->mtx);
  422. return 0;
  423. }
  424. static int tas_snd_capture_source_put(struct snd_kcontrol *kcontrol,
  425. struct snd_ctl_elem_value *ucontrol)
  426. {
  427. struct tas *tas = snd_kcontrol_chip(kcontrol);
  428. int oldacr;
  429. if (ucontrol->value.enumerated.item[0] > 1)
  430. return -EINVAL;
  431. mutex_lock(&tas->mtx);
  432. oldacr = tas->acr;
  433. /*
  434. * Despite what the data sheet says in one place, the
  435. * TAS_ACR_B_MONAUREAL bit forces mono output even when
  436. * input A (line in) is selected.
  437. */
  438. tas->acr &= ~(TAS_ACR_INPUT_B | TAS_ACR_B_MONAUREAL);
  439. if (ucontrol->value.enumerated.item[0])
  440. tas->acr |= TAS_ACR_INPUT_B | TAS_ACR_B_MONAUREAL |
  441. TAS_ACR_B_MON_SEL_RIGHT;
  442. if (oldacr == tas->acr) {
  443. mutex_unlock(&tas->mtx);
  444. return 0;
  445. }
  446. if (tas->hw_enabled)
  447. tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
  448. mutex_unlock(&tas->mtx);
  449. return 1;
  450. }
  451. static const struct snd_kcontrol_new capture_source_control = {
  452. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  453. /* If we name this 'Input Source', it properly shows up in
  454. * alsamixer as a selection, * but it's shown under the
  455. * 'Playback' category.
  456. * If I name it 'Capture Source', it shows up in strange
  457. * ways (two bools of which one can be selected at a
  458. * time) but at least it's shown in the 'Capture'
  459. * category.
  460. * I was told that this was due to backward compatibility,
  461. * but I don't understand then why the mangling is *not*
  462. * done when I name it "Input Source".....
  463. */
  464. .name = "Capture Source",
  465. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  466. .info = tas_snd_capture_source_info,
  467. .get = tas_snd_capture_source_get,
  468. .put = tas_snd_capture_source_put,
  469. };
  470. static int tas_snd_treble_info(struct snd_kcontrol *kcontrol,
  471. struct snd_ctl_elem_info *uinfo)
  472. {
  473. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  474. uinfo->count = 1;
  475. uinfo->value.integer.min = TAS3004_TREBLE_MIN;
  476. uinfo->value.integer.max = TAS3004_TREBLE_MAX;
  477. return 0;
  478. }
  479. static int tas_snd_treble_get(struct snd_kcontrol *kcontrol,
  480. struct snd_ctl_elem_value *ucontrol)
  481. {
  482. struct tas *tas = snd_kcontrol_chip(kcontrol);
  483. mutex_lock(&tas->mtx);
  484. ucontrol->value.integer.value[0] = tas->treble;
  485. mutex_unlock(&tas->mtx);
  486. return 0;
  487. }
  488. static int tas_snd_treble_put(struct snd_kcontrol *kcontrol,
  489. struct snd_ctl_elem_value *ucontrol)
  490. {
  491. struct tas *tas = snd_kcontrol_chip(kcontrol);
  492. if (ucontrol->value.integer.value[0] < TAS3004_TREBLE_MIN ||
  493. ucontrol->value.integer.value[0] > TAS3004_TREBLE_MAX)
  494. return -EINVAL;
  495. mutex_lock(&tas->mtx);
  496. if (tas->treble == ucontrol->value.integer.value[0]) {
  497. mutex_unlock(&tas->mtx);
  498. return 0;
  499. }
  500. tas->treble = ucontrol->value.integer.value[0];
  501. if (tas->hw_enabled)
  502. tas_set_treble(tas);
  503. mutex_unlock(&tas->mtx);
  504. return 1;
  505. }
  506. static const struct snd_kcontrol_new treble_control = {
  507. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  508. .name = "Treble",
  509. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  510. .info = tas_snd_treble_info,
  511. .get = tas_snd_treble_get,
  512. .put = tas_snd_treble_put,
  513. };
  514. static int tas_snd_bass_info(struct snd_kcontrol *kcontrol,
  515. struct snd_ctl_elem_info *uinfo)
  516. {
  517. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  518. uinfo->count = 1;
  519. uinfo->value.integer.min = TAS3004_BASS_MIN;
  520. uinfo->value.integer.max = TAS3004_BASS_MAX;
  521. return 0;
  522. }
  523. static int tas_snd_bass_get(struct snd_kcontrol *kcontrol,
  524. struct snd_ctl_elem_value *ucontrol)
  525. {
  526. struct tas *tas = snd_kcontrol_chip(kcontrol);
  527. mutex_lock(&tas->mtx);
  528. ucontrol->value.integer.value[0] = tas->bass;
  529. mutex_unlock(&tas->mtx);
  530. return 0;
  531. }
  532. static int tas_snd_bass_put(struct snd_kcontrol *kcontrol,
  533. struct snd_ctl_elem_value *ucontrol)
  534. {
  535. struct tas *tas = snd_kcontrol_chip(kcontrol);
  536. if (ucontrol->value.integer.value[0] < TAS3004_BASS_MIN ||
  537. ucontrol->value.integer.value[0] > TAS3004_BASS_MAX)
  538. return -EINVAL;
  539. mutex_lock(&tas->mtx);
  540. if (tas->bass == ucontrol->value.integer.value[0]) {
  541. mutex_unlock(&tas->mtx);
  542. return 0;
  543. }
  544. tas->bass = ucontrol->value.integer.value[0];
  545. if (tas->hw_enabled)
  546. tas_set_bass(tas);
  547. mutex_unlock(&tas->mtx);
  548. return 1;
  549. }
  550. static const struct snd_kcontrol_new bass_control = {
  551. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  552. .name = "Bass",
  553. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  554. .info = tas_snd_bass_info,
  555. .get = tas_snd_bass_get,
  556. .put = tas_snd_bass_put,
  557. };
  558. static struct transfer_info tas_transfers[] = {
  559. {
  560. /* input */
  561. .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S24_BE,
  562. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  563. .transfer_in = 1,
  564. },
  565. {
  566. /* output */
  567. .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S24_BE,
  568. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  569. .transfer_in = 0,
  570. },
  571. {}
  572. };
  573. static int tas_usable(struct codec_info_item *cii,
  574. struct transfer_info *ti,
  575. struct transfer_info *out)
  576. {
  577. return 1;
  578. }
  579. static int tas_reset_init(struct tas *tas)
  580. {
  581. u8 tmp;
  582. tas->codec.gpio->methods->all_amps_off(tas->codec.gpio);
  583. msleep(5);
  584. tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
  585. msleep(5);
  586. tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 1);
  587. msleep(20);
  588. tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
  589. msleep(10);
  590. tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio);
  591. tmp = TAS_MCS_SCLK64 | TAS_MCS_SPORT_MODE_I2S | TAS_MCS_SPORT_WL_24BIT;
  592. if (tas_write_reg(tas, TAS_REG_MCS, 1, &tmp))
  593. goto outerr;
  594. tas->acr |= TAS_ACR_ANALOG_PDOWN;
  595. if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr))
  596. goto outerr;
  597. tmp = 0;
  598. if (tas_write_reg(tas, TAS_REG_MCS2, 1, &tmp))
  599. goto outerr;
  600. tas3004_set_drc(tas);
  601. /* Set treble & bass to 0dB */
  602. tas->treble = TAS3004_TREBLE_ZERO;
  603. tas->bass = TAS3004_BASS_ZERO;
  604. tas_set_treble(tas);
  605. tas_set_bass(tas);
  606. tas->acr &= ~TAS_ACR_ANALOG_PDOWN;
  607. if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr))
  608. goto outerr;
  609. return 0;
  610. outerr:
  611. return -ENODEV;
  612. }
  613. static int tas_switch_clock(struct codec_info_item *cii, enum clock_switch clock)
  614. {
  615. struct tas *tas = cii->codec_data;
  616. switch(clock) {
  617. case CLOCK_SWITCH_PREPARE_SLAVE:
  618. /* Clocks are going away, mute mute mute */
  619. tas->codec.gpio->methods->all_amps_off(tas->codec.gpio);
  620. tas->hw_enabled = 0;
  621. break;
  622. case CLOCK_SWITCH_SLAVE:
  623. /* Clocks are back, re-init the codec */
  624. mutex_lock(&tas->mtx);
  625. tas_reset_init(tas);
  626. tas_set_volume(tas);
  627. tas_set_mixer(tas);
  628. tas->hw_enabled = 1;
  629. tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio);
  630. mutex_unlock(&tas->mtx);
  631. break;
  632. default:
  633. /* doesn't happen as of now */
  634. return -EINVAL;
  635. }
  636. return 0;
  637. }
  638. #ifdef CONFIG_PM
  639. /* we are controlled via i2c and assume that is always up
  640. * If that wasn't the case, we'd have to suspend once
  641. * our i2c device is suspended, and then take note of that! */
  642. static int tas_suspend(struct tas *tas)
  643. {
  644. mutex_lock(&tas->mtx);
  645. tas->hw_enabled = 0;
  646. tas->acr |= TAS_ACR_ANALOG_PDOWN;
  647. tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
  648. mutex_unlock(&tas->mtx);
  649. return 0;
  650. }
  651. static int tas_resume(struct tas *tas)
  652. {
  653. /* reset codec */
  654. mutex_lock(&tas->mtx);
  655. tas_reset_init(tas);
  656. tas_set_volume(tas);
  657. tas_set_mixer(tas);
  658. tas->hw_enabled = 1;
  659. mutex_unlock(&tas->mtx);
  660. return 0;
  661. }
  662. static int _tas_suspend(struct codec_info_item *cii, pm_message_t state)
  663. {
  664. return tas_suspend(cii->codec_data);
  665. }
  666. static int _tas_resume(struct codec_info_item *cii)
  667. {
  668. return tas_resume(cii->codec_data);
  669. }
  670. #else /* CONFIG_PM */
  671. #define _tas_suspend NULL
  672. #define _tas_resume NULL
  673. #endif /* CONFIG_PM */
  674. static struct codec_info tas_codec_info = {
  675. .transfers = tas_transfers,
  676. /* in theory, we can drive it at 512 too...
  677. * but so far the framework doesn't allow
  678. * for that and I don't see much point in it. */
  679. .sysclock_factor = 256,
  680. /* same here, could be 32 for just one 16 bit format */
  681. .bus_factor = 64,
  682. .owner = THIS_MODULE,
  683. .usable = tas_usable,
  684. .switch_clock = tas_switch_clock,
  685. .suspend = _tas_suspend,
  686. .resume = _tas_resume,
  687. };
  688. static int tas_init_codec(struct aoa_codec *codec)
  689. {
  690. struct tas *tas = codec_to_tas(codec);
  691. int err;
  692. if (!tas->codec.gpio || !tas->codec.gpio->methods) {
  693. printk(KERN_ERR PFX "gpios not assigned!!\n");
  694. return -EINVAL;
  695. }
  696. mutex_lock(&tas->mtx);
  697. if (tas_reset_init(tas)) {
  698. printk(KERN_ERR PFX "tas failed to initialise\n");
  699. mutex_unlock(&tas->mtx);
  700. return -ENXIO;
  701. }
  702. tas->hw_enabled = 1;
  703. mutex_unlock(&tas->mtx);
  704. if (tas->codec.soundbus_dev->attach_codec(tas->codec.soundbus_dev,
  705. aoa_get_card(),
  706. &tas_codec_info, tas)) {
  707. printk(KERN_ERR PFX "error attaching tas to soundbus\n");
  708. return -ENODEV;
  709. }
  710. if (aoa_snd_device_new(SNDRV_DEV_CODEC, tas, &ops)) {
  711. printk(KERN_ERR PFX "failed to create tas snd device!\n");
  712. return -ENODEV;
  713. }
  714. err = aoa_snd_ctl_add(snd_ctl_new1(&volume_control, tas));
  715. if (err)
  716. goto error;
  717. err = aoa_snd_ctl_add(snd_ctl_new1(&mute_control, tas));
  718. if (err)
  719. goto error;
  720. err = aoa_snd_ctl_add(snd_ctl_new1(&pcm1_control, tas));
  721. if (err)
  722. goto error;
  723. err = aoa_snd_ctl_add(snd_ctl_new1(&monitor_control, tas));
  724. if (err)
  725. goto error;
  726. err = aoa_snd_ctl_add(snd_ctl_new1(&capture_source_control, tas));
  727. if (err)
  728. goto error;
  729. err = aoa_snd_ctl_add(snd_ctl_new1(&drc_range_control, tas));
  730. if (err)
  731. goto error;
  732. err = aoa_snd_ctl_add(snd_ctl_new1(&drc_switch_control, tas));
  733. if (err)
  734. goto error;
  735. err = aoa_snd_ctl_add(snd_ctl_new1(&treble_control, tas));
  736. if (err)
  737. goto error;
  738. err = aoa_snd_ctl_add(snd_ctl_new1(&bass_control, tas));
  739. if (err)
  740. goto error;
  741. return 0;
  742. error:
  743. tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
  744. snd_device_free(aoa_get_card(), tas);
  745. return err;
  746. }
  747. static void tas_exit_codec(struct aoa_codec *codec)
  748. {
  749. struct tas *tas = codec_to_tas(codec);
  750. if (!tas->codec.soundbus_dev)
  751. return;
  752. tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
  753. }
  754. static int tas_i2c_probe(struct i2c_client *client,
  755. const struct i2c_device_id *id)
  756. {
  757. struct device_node *node = client->dev.of_node;
  758. struct tas *tas;
  759. tas = kzalloc(sizeof(struct tas), GFP_KERNEL);
  760. if (!tas)
  761. return -ENOMEM;
  762. mutex_init(&tas->mtx);
  763. tas->i2c = client;
  764. i2c_set_clientdata(client, tas);
  765. /* seems that half is a saner default */
  766. tas->drc_range = TAS3004_DRC_MAX / 2;
  767. strscpy(tas->codec.name, "tas", MAX_CODEC_NAME_LEN);
  768. tas->codec.owner = THIS_MODULE;
  769. tas->codec.init = tas_init_codec;
  770. tas->codec.exit = tas_exit_codec;
  771. tas->codec.node = of_node_get(node);
  772. if (aoa_codec_register(&tas->codec)) {
  773. goto fail;
  774. }
  775. printk(KERN_DEBUG
  776. "snd-aoa-codec-tas: tas found, addr 0x%02x on %pOF\n",
  777. (unsigned int)client->addr, node);
  778. return 0;
  779. fail:
  780. mutex_destroy(&tas->mtx);
  781. kfree(tas);
  782. return -EINVAL;
  783. }
  784. static void tas_i2c_remove(struct i2c_client *client)
  785. {
  786. struct tas *tas = i2c_get_clientdata(client);
  787. u8 tmp = TAS_ACR_ANALOG_PDOWN;
  788. aoa_codec_unregister(&tas->codec);
  789. of_node_put(tas->codec.node);
  790. /* power down codec chip */
  791. tas_write_reg(tas, TAS_REG_ACR, 1, &tmp);
  792. mutex_destroy(&tas->mtx);
  793. kfree(tas);
  794. }
  795. static const struct i2c_device_id tas_i2c_id[] = {
  796. { "MAC,tas3004", 0 },
  797. { }
  798. };
  799. MODULE_DEVICE_TABLE(i2c,tas_i2c_id);
  800. static struct i2c_driver tas_driver = {
  801. .driver = {
  802. .name = "aoa_codec_tas",
  803. },
  804. .probe = tas_i2c_probe,
  805. .remove = tas_i2c_remove,
  806. .id_table = tas_i2c_id,
  807. };
  808. module_i2c_driver(tas_driver);