tas.c 24 KB

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