snd-aoa-codec-tas.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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. MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
  70. MODULE_LICENSE("GPL");
  71. MODULE_DESCRIPTION("tas codec driver for snd-aoa");
  72. #include "snd-aoa-codec-tas.h"
  73. #include "snd-aoa-codec-tas-gain-table.h"
  74. #include "snd-aoa-codec-tas-basstreble.h"
  75. #include "../aoa.h"
  76. #include "../soundbus/soundbus.h"
  77. #define PFX "snd-aoa-codec-tas: "
  78. struct tas {
  79. struct aoa_codec codec;
  80. struct i2c_client i2c;
  81. u32 mute_l:1, mute_r:1 ,
  82. controls_created:1 ,
  83. drc_enabled:1,
  84. hw_enabled:1;
  85. u8 cached_volume_l, cached_volume_r;
  86. u8 mixer_l[3], mixer_r[3];
  87. u8 bass, treble;
  88. u8 acr;
  89. int drc_range;
  90. /* protects hardware access against concurrency from
  91. * userspace when hitting controls and during
  92. * codec init/suspend/resume */
  93. struct mutex mtx;
  94. };
  95. static int tas_reset_init(struct tas *tas);
  96. static struct tas *codec_to_tas(struct aoa_codec *codec)
  97. {
  98. return container_of(codec, struct tas, codec);
  99. }
  100. static inline int tas_write_reg(struct tas *tas, u8 reg, u8 len, u8 *data)
  101. {
  102. if (len == 1)
  103. return i2c_smbus_write_byte_data(&tas->i2c, reg, *data);
  104. else
  105. return i2c_smbus_write_i2c_block_data(&tas->i2c, reg, len, data);
  106. }
  107. static void tas3004_set_drc(struct tas *tas)
  108. {
  109. unsigned char val[6];
  110. if (tas->drc_enabled)
  111. val[0] = 0x50; /* 3:1 above threshold */
  112. else
  113. val[0] = 0x51; /* disabled */
  114. val[1] = 0x02; /* 1:1 below threshold */
  115. if (tas->drc_range > 0xef)
  116. val[2] = 0xef;
  117. else if (tas->drc_range < 0)
  118. val[2] = 0x00;
  119. else
  120. val[2] = tas->drc_range;
  121. val[3] = 0xb0;
  122. val[4] = 0x60;
  123. val[5] = 0xa0;
  124. tas_write_reg(tas, TAS_REG_DRC, 6, val);
  125. }
  126. static void tas_set_treble(struct tas *tas)
  127. {
  128. u8 tmp;
  129. tmp = tas3004_treble(tas->treble);
  130. tas_write_reg(tas, TAS_REG_TREBLE, 1, &tmp);
  131. }
  132. static void tas_set_bass(struct tas *tas)
  133. {
  134. u8 tmp;
  135. tmp = tas3004_bass(tas->bass);
  136. tas_write_reg(tas, TAS_REG_BASS, 1, &tmp);
  137. }
  138. static void tas_set_volume(struct tas *tas)
  139. {
  140. u8 block[6];
  141. int tmp;
  142. u8 left, right;
  143. left = tas->cached_volume_l;
  144. right = tas->cached_volume_r;
  145. if (left > 177) left = 177;
  146. if (right > 177) right = 177;
  147. if (tas->mute_l) left = 0;
  148. if (tas->mute_r) right = 0;
  149. /* analysing the volume and mixer tables shows
  150. * that they are similar enough when we shift
  151. * the mixer table down by 4 bits. The error
  152. * is miniscule, in just one item the error
  153. * is 1, at a value of 0x07f17b (mixer table
  154. * value is 0x07f17a) */
  155. tmp = tas_gaintable[left];
  156. block[0] = tmp>>20;
  157. block[1] = tmp>>12;
  158. block[2] = tmp>>4;
  159. tmp = tas_gaintable[right];
  160. block[3] = tmp>>20;
  161. block[4] = tmp>>12;
  162. block[5] = tmp>>4;
  163. tas_write_reg(tas, TAS_REG_VOL, 6, block);
  164. }
  165. static void tas_set_mixer(struct tas *tas)
  166. {
  167. u8 block[9];
  168. int tmp, i;
  169. u8 val;
  170. for (i=0;i<3;i++) {
  171. val = tas->mixer_l[i];
  172. if (val > 177) val = 177;
  173. tmp = tas_gaintable[val];
  174. block[3*i+0] = tmp>>16;
  175. block[3*i+1] = tmp>>8;
  176. block[3*i+2] = tmp;
  177. }
  178. tas_write_reg(tas, TAS_REG_LMIX, 9, block);
  179. for (i=0;i<3;i++) {
  180. val = tas->mixer_r[i];
  181. if (val > 177) val = 177;
  182. tmp = tas_gaintable[val];
  183. block[3*i+0] = tmp>>16;
  184. block[3*i+1] = tmp>>8;
  185. block[3*i+2] = tmp;
  186. }
  187. tas_write_reg(tas, TAS_REG_RMIX, 9, block);
  188. }
  189. /* alsa stuff */
  190. static int tas_dev_register(struct snd_device *dev)
  191. {
  192. return 0;
  193. }
  194. static struct snd_device_ops ops = {
  195. .dev_register = tas_dev_register,
  196. };
  197. static int tas_snd_vol_info(struct snd_kcontrol *kcontrol,
  198. struct snd_ctl_elem_info *uinfo)
  199. {
  200. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  201. uinfo->count = 2;
  202. uinfo->value.integer.min = 0;
  203. uinfo->value.integer.max = 177;
  204. return 0;
  205. }
  206. static int tas_snd_vol_get(struct snd_kcontrol *kcontrol,
  207. struct snd_ctl_elem_value *ucontrol)
  208. {
  209. struct tas *tas = snd_kcontrol_chip(kcontrol);
  210. mutex_lock(&tas->mtx);
  211. ucontrol->value.integer.value[0] = tas->cached_volume_l;
  212. ucontrol->value.integer.value[1] = tas->cached_volume_r;
  213. mutex_unlock(&tas->mtx);
  214. return 0;
  215. }
  216. static int tas_snd_vol_put(struct snd_kcontrol *kcontrol,
  217. struct snd_ctl_elem_value *ucontrol)
  218. {
  219. struct tas *tas = snd_kcontrol_chip(kcontrol);
  220. if (ucontrol->value.integer.value[0] < 0 ||
  221. ucontrol->value.integer.value[0] > 177)
  222. return -EINVAL;
  223. if (ucontrol->value.integer.value[1] < 0 ||
  224. ucontrol->value.integer.value[1] > 177)
  225. return -EINVAL;
  226. mutex_lock(&tas->mtx);
  227. if (tas->cached_volume_l == ucontrol->value.integer.value[0]
  228. && tas->cached_volume_r == ucontrol->value.integer.value[1]) {
  229. mutex_unlock(&tas->mtx);
  230. return 0;
  231. }
  232. tas->cached_volume_l = ucontrol->value.integer.value[0];
  233. tas->cached_volume_r = ucontrol->value.integer.value[1];
  234. if (tas->hw_enabled)
  235. tas_set_volume(tas);
  236. mutex_unlock(&tas->mtx);
  237. return 1;
  238. }
  239. static struct snd_kcontrol_new volume_control = {
  240. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  241. .name = "Master Playback Volume",
  242. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  243. .info = tas_snd_vol_info,
  244. .get = tas_snd_vol_get,
  245. .put = tas_snd_vol_put,
  246. };
  247. #define tas_snd_mute_info snd_ctl_boolean_stereo_info
  248. static int tas_snd_mute_get(struct snd_kcontrol *kcontrol,
  249. struct snd_ctl_elem_value *ucontrol)
  250. {
  251. struct tas *tas = snd_kcontrol_chip(kcontrol);
  252. mutex_lock(&tas->mtx);
  253. ucontrol->value.integer.value[0] = !tas->mute_l;
  254. ucontrol->value.integer.value[1] = !tas->mute_r;
  255. mutex_unlock(&tas->mtx);
  256. return 0;
  257. }
  258. static int tas_snd_mute_put(struct snd_kcontrol *kcontrol,
  259. struct snd_ctl_elem_value *ucontrol)
  260. {
  261. struct tas *tas = snd_kcontrol_chip(kcontrol);
  262. mutex_lock(&tas->mtx);
  263. if (tas->mute_l == !ucontrol->value.integer.value[0]
  264. && tas->mute_r == !ucontrol->value.integer.value[1]) {
  265. mutex_unlock(&tas->mtx);
  266. return 0;
  267. }
  268. tas->mute_l = !ucontrol->value.integer.value[0];
  269. tas->mute_r = !ucontrol->value.integer.value[1];
  270. if (tas->hw_enabled)
  271. tas_set_volume(tas);
  272. mutex_unlock(&tas->mtx);
  273. return 1;
  274. }
  275. static struct snd_kcontrol_new mute_control = {
  276. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  277. .name = "Master Playback Switch",
  278. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  279. .info = tas_snd_mute_info,
  280. .get = tas_snd_mute_get,
  281. .put = tas_snd_mute_put,
  282. };
  283. static int tas_snd_mixer_info(struct snd_kcontrol *kcontrol,
  284. struct snd_ctl_elem_info *uinfo)
  285. {
  286. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  287. uinfo->count = 2;
  288. uinfo->value.integer.min = 0;
  289. uinfo->value.integer.max = 177;
  290. return 0;
  291. }
  292. static int tas_snd_mixer_get(struct snd_kcontrol *kcontrol,
  293. struct snd_ctl_elem_value *ucontrol)
  294. {
  295. struct tas *tas = snd_kcontrol_chip(kcontrol);
  296. int idx = kcontrol->private_value;
  297. mutex_lock(&tas->mtx);
  298. ucontrol->value.integer.value[0] = tas->mixer_l[idx];
  299. ucontrol->value.integer.value[1] = tas->mixer_r[idx];
  300. mutex_unlock(&tas->mtx);
  301. return 0;
  302. }
  303. static int tas_snd_mixer_put(struct snd_kcontrol *kcontrol,
  304. struct snd_ctl_elem_value *ucontrol)
  305. {
  306. struct tas *tas = snd_kcontrol_chip(kcontrol);
  307. int idx = kcontrol->private_value;
  308. mutex_lock(&tas->mtx);
  309. if (tas->mixer_l[idx] == ucontrol->value.integer.value[0]
  310. && tas->mixer_r[idx] == ucontrol->value.integer.value[1]) {
  311. mutex_unlock(&tas->mtx);
  312. return 0;
  313. }
  314. tas->mixer_l[idx] = ucontrol->value.integer.value[0];
  315. tas->mixer_r[idx] = ucontrol->value.integer.value[1];
  316. if (tas->hw_enabled)
  317. tas_set_mixer(tas);
  318. mutex_unlock(&tas->mtx);
  319. return 1;
  320. }
  321. #define MIXER_CONTROL(n,descr,idx) \
  322. static struct snd_kcontrol_new n##_control = { \
  323. .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  324. .name = descr " Playback Volume", \
  325. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
  326. .info = tas_snd_mixer_info, \
  327. .get = tas_snd_mixer_get, \
  328. .put = tas_snd_mixer_put, \
  329. .private_value = idx, \
  330. }
  331. MIXER_CONTROL(pcm1, "PCM", 0);
  332. MIXER_CONTROL(monitor, "Monitor", 2);
  333. static int tas_snd_drc_range_info(struct snd_kcontrol *kcontrol,
  334. struct snd_ctl_elem_info *uinfo)
  335. {
  336. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  337. uinfo->count = 1;
  338. uinfo->value.integer.min = 0;
  339. uinfo->value.integer.max = TAS3004_DRC_MAX;
  340. return 0;
  341. }
  342. static int tas_snd_drc_range_get(struct snd_kcontrol *kcontrol,
  343. struct snd_ctl_elem_value *ucontrol)
  344. {
  345. struct tas *tas = snd_kcontrol_chip(kcontrol);
  346. mutex_lock(&tas->mtx);
  347. ucontrol->value.integer.value[0] = tas->drc_range;
  348. mutex_unlock(&tas->mtx);
  349. return 0;
  350. }
  351. static int tas_snd_drc_range_put(struct snd_kcontrol *kcontrol,
  352. struct snd_ctl_elem_value *ucontrol)
  353. {
  354. struct tas *tas = snd_kcontrol_chip(kcontrol);
  355. if (ucontrol->value.integer.value[0] < 0 ||
  356. ucontrol->value.integer.value[0] > TAS3004_DRC_MAX)
  357. return -EINVAL;
  358. mutex_lock(&tas->mtx);
  359. if (tas->drc_range == ucontrol->value.integer.value[0]) {
  360. mutex_unlock(&tas->mtx);
  361. return 0;
  362. }
  363. tas->drc_range = ucontrol->value.integer.value[0];
  364. if (tas->hw_enabled)
  365. tas3004_set_drc(tas);
  366. mutex_unlock(&tas->mtx);
  367. return 1;
  368. }
  369. static struct snd_kcontrol_new drc_range_control = {
  370. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  371. .name = "DRC Range",
  372. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  373. .info = tas_snd_drc_range_info,
  374. .get = tas_snd_drc_range_get,
  375. .put = tas_snd_drc_range_put,
  376. };
  377. #define tas_snd_drc_switch_info snd_ctl_boolean_mono_info
  378. static int tas_snd_drc_switch_get(struct snd_kcontrol *kcontrol,
  379. struct snd_ctl_elem_value *ucontrol)
  380. {
  381. struct tas *tas = snd_kcontrol_chip(kcontrol);
  382. mutex_lock(&tas->mtx);
  383. ucontrol->value.integer.value[0] = tas->drc_enabled;
  384. mutex_unlock(&tas->mtx);
  385. return 0;
  386. }
  387. static int tas_snd_drc_switch_put(struct snd_kcontrol *kcontrol,
  388. struct snd_ctl_elem_value *ucontrol)
  389. {
  390. struct tas *tas = snd_kcontrol_chip(kcontrol);
  391. mutex_lock(&tas->mtx);
  392. if (tas->drc_enabled == ucontrol->value.integer.value[0]) {
  393. mutex_unlock(&tas->mtx);
  394. return 0;
  395. }
  396. tas->drc_enabled = !!ucontrol->value.integer.value[0];
  397. if (tas->hw_enabled)
  398. tas3004_set_drc(tas);
  399. mutex_unlock(&tas->mtx);
  400. return 1;
  401. }
  402. static struct snd_kcontrol_new drc_switch_control = {
  403. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  404. .name = "DRC Range Switch",
  405. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  406. .info = tas_snd_drc_switch_info,
  407. .get = tas_snd_drc_switch_get,
  408. .put = tas_snd_drc_switch_put,
  409. };
  410. static int tas_snd_capture_source_info(struct snd_kcontrol *kcontrol,
  411. struct snd_ctl_elem_info *uinfo)
  412. {
  413. static char *texts[] = { "Line-In", "Microphone" };
  414. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  415. uinfo->count = 1;
  416. uinfo->value.enumerated.items = 2;
  417. if (uinfo->value.enumerated.item > 1)
  418. uinfo->value.enumerated.item = 1;
  419. strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  420. return 0;
  421. }
  422. static int tas_snd_capture_source_get(struct snd_kcontrol *kcontrol,
  423. struct snd_ctl_elem_value *ucontrol)
  424. {
  425. struct tas *tas = snd_kcontrol_chip(kcontrol);
  426. mutex_lock(&tas->mtx);
  427. ucontrol->value.enumerated.item[0] = !!(tas->acr & TAS_ACR_INPUT_B);
  428. mutex_unlock(&tas->mtx);
  429. return 0;
  430. }
  431. static int tas_snd_capture_source_put(struct snd_kcontrol *kcontrol,
  432. struct snd_ctl_elem_value *ucontrol)
  433. {
  434. struct tas *tas = snd_kcontrol_chip(kcontrol);
  435. int oldacr;
  436. if (ucontrol->value.enumerated.item[0] > 1)
  437. return -EINVAL;
  438. mutex_lock(&tas->mtx);
  439. oldacr = tas->acr;
  440. /*
  441. * Despite what the data sheet says in one place, the
  442. * TAS_ACR_B_MONAUREAL bit forces mono output even when
  443. * input A (line in) is selected.
  444. */
  445. tas->acr &= ~(TAS_ACR_INPUT_B | TAS_ACR_B_MONAUREAL);
  446. if (ucontrol->value.enumerated.item[0])
  447. tas->acr |= TAS_ACR_INPUT_B | TAS_ACR_B_MONAUREAL |
  448. TAS_ACR_B_MON_SEL_RIGHT;
  449. if (oldacr == tas->acr) {
  450. mutex_unlock(&tas->mtx);
  451. return 0;
  452. }
  453. if (tas->hw_enabled)
  454. tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
  455. mutex_unlock(&tas->mtx);
  456. return 1;
  457. }
  458. static struct snd_kcontrol_new capture_source_control = {
  459. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  460. /* If we name this 'Input Source', it properly shows up in
  461. * alsamixer as a selection, * but it's shown under the
  462. * 'Playback' category.
  463. * If I name it 'Capture Source', it shows up in strange
  464. * ways (two bools of which one can be selected at a
  465. * time) but at least it's shown in the 'Capture'
  466. * category.
  467. * I was told that this was due to backward compatibility,
  468. * but I don't understand then why the mangling is *not*
  469. * done when I name it "Input Source".....
  470. */
  471. .name = "Capture Source",
  472. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  473. .info = tas_snd_capture_source_info,
  474. .get = tas_snd_capture_source_get,
  475. .put = tas_snd_capture_source_put,
  476. };
  477. static int tas_snd_treble_info(struct snd_kcontrol *kcontrol,
  478. struct snd_ctl_elem_info *uinfo)
  479. {
  480. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  481. uinfo->count = 1;
  482. uinfo->value.integer.min = TAS3004_TREBLE_MIN;
  483. uinfo->value.integer.max = TAS3004_TREBLE_MAX;
  484. return 0;
  485. }
  486. static int tas_snd_treble_get(struct snd_kcontrol *kcontrol,
  487. struct snd_ctl_elem_value *ucontrol)
  488. {
  489. struct tas *tas = snd_kcontrol_chip(kcontrol);
  490. mutex_lock(&tas->mtx);
  491. ucontrol->value.integer.value[0] = tas->treble;
  492. mutex_unlock(&tas->mtx);
  493. return 0;
  494. }
  495. static int tas_snd_treble_put(struct snd_kcontrol *kcontrol,
  496. struct snd_ctl_elem_value *ucontrol)
  497. {
  498. struct tas *tas = snd_kcontrol_chip(kcontrol);
  499. if (ucontrol->value.integer.value[0] < TAS3004_TREBLE_MIN ||
  500. ucontrol->value.integer.value[0] > TAS3004_TREBLE_MAX)
  501. return -EINVAL;
  502. mutex_lock(&tas->mtx);
  503. if (tas->treble == ucontrol->value.integer.value[0]) {
  504. mutex_unlock(&tas->mtx);
  505. return 0;
  506. }
  507. tas->treble = ucontrol->value.integer.value[0];
  508. if (tas->hw_enabled)
  509. tas_set_treble(tas);
  510. mutex_unlock(&tas->mtx);
  511. return 1;
  512. }
  513. static struct snd_kcontrol_new treble_control = {
  514. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  515. .name = "Treble",
  516. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  517. .info = tas_snd_treble_info,
  518. .get = tas_snd_treble_get,
  519. .put = tas_snd_treble_put,
  520. };
  521. static int tas_snd_bass_info(struct snd_kcontrol *kcontrol,
  522. struct snd_ctl_elem_info *uinfo)
  523. {
  524. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  525. uinfo->count = 1;
  526. uinfo->value.integer.min = TAS3004_BASS_MIN;
  527. uinfo->value.integer.max = TAS3004_BASS_MAX;
  528. return 0;
  529. }
  530. static int tas_snd_bass_get(struct snd_kcontrol *kcontrol,
  531. struct snd_ctl_elem_value *ucontrol)
  532. {
  533. struct tas *tas = snd_kcontrol_chip(kcontrol);
  534. mutex_lock(&tas->mtx);
  535. ucontrol->value.integer.value[0] = tas->bass;
  536. mutex_unlock(&tas->mtx);
  537. return 0;
  538. }
  539. static int tas_snd_bass_put(struct snd_kcontrol *kcontrol,
  540. struct snd_ctl_elem_value *ucontrol)
  541. {
  542. struct tas *tas = snd_kcontrol_chip(kcontrol);
  543. if (ucontrol->value.integer.value[0] < TAS3004_BASS_MIN ||
  544. ucontrol->value.integer.value[0] > TAS3004_BASS_MAX)
  545. return -EINVAL;
  546. mutex_lock(&tas->mtx);
  547. if (tas->bass == ucontrol->value.integer.value[0]) {
  548. mutex_unlock(&tas->mtx);
  549. return 0;
  550. }
  551. tas->bass = ucontrol->value.integer.value[0];
  552. if (tas->hw_enabled)
  553. tas_set_bass(tas);
  554. mutex_unlock(&tas->mtx);
  555. return 1;
  556. }
  557. static struct snd_kcontrol_new bass_control = {
  558. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  559. .name = "Bass",
  560. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  561. .info = tas_snd_bass_info,
  562. .get = tas_snd_bass_get,
  563. .put = tas_snd_bass_put,
  564. };
  565. static struct transfer_info tas_transfers[] = {
  566. {
  567. /* input */
  568. .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S24_BE,
  569. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  570. .transfer_in = 1,
  571. },
  572. {
  573. /* output */
  574. .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S24_BE,
  575. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  576. .transfer_in = 0,
  577. },
  578. {}
  579. };
  580. static int tas_usable(struct codec_info_item *cii,
  581. struct transfer_info *ti,
  582. struct transfer_info *out)
  583. {
  584. return 1;
  585. }
  586. static int tas_reset_init(struct tas *tas)
  587. {
  588. u8 tmp;
  589. tas->codec.gpio->methods->all_amps_off(tas->codec.gpio);
  590. msleep(5);
  591. tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
  592. msleep(5);
  593. tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 1);
  594. msleep(20);
  595. tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
  596. msleep(10);
  597. tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio);
  598. tmp = TAS_MCS_SCLK64 | TAS_MCS_SPORT_MODE_I2S | TAS_MCS_SPORT_WL_24BIT;
  599. if (tas_write_reg(tas, TAS_REG_MCS, 1, &tmp))
  600. goto outerr;
  601. tas->acr |= TAS_ACR_ANALOG_PDOWN;
  602. if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr))
  603. goto outerr;
  604. tmp = 0;
  605. if (tas_write_reg(tas, TAS_REG_MCS2, 1, &tmp))
  606. goto outerr;
  607. tas3004_set_drc(tas);
  608. /* Set treble & bass to 0dB */
  609. tas->treble = TAS3004_TREBLE_ZERO;
  610. tas->bass = TAS3004_BASS_ZERO;
  611. tas_set_treble(tas);
  612. tas_set_bass(tas);
  613. tas->acr &= ~TAS_ACR_ANALOG_PDOWN;
  614. if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr))
  615. goto outerr;
  616. return 0;
  617. outerr:
  618. return -ENODEV;
  619. }
  620. static int tas_switch_clock(struct codec_info_item *cii, enum clock_switch clock)
  621. {
  622. struct tas *tas = cii->codec_data;
  623. switch(clock) {
  624. case CLOCK_SWITCH_PREPARE_SLAVE:
  625. /* Clocks are going away, mute mute mute */
  626. tas->codec.gpio->methods->all_amps_off(tas->codec.gpio);
  627. tas->hw_enabled = 0;
  628. break;
  629. case CLOCK_SWITCH_SLAVE:
  630. /* Clocks are back, re-init the codec */
  631. mutex_lock(&tas->mtx);
  632. tas_reset_init(tas);
  633. tas_set_volume(tas);
  634. tas_set_mixer(tas);
  635. tas->hw_enabled = 1;
  636. tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio);
  637. mutex_unlock(&tas->mtx);
  638. break;
  639. default:
  640. /* doesn't happen as of now */
  641. return -EINVAL;
  642. }
  643. return 0;
  644. }
  645. #ifdef CONFIG_PM
  646. /* we are controlled via i2c and assume that is always up
  647. * If that wasn't the case, we'd have to suspend once
  648. * our i2c device is suspended, and then take note of that! */
  649. static int tas_suspend(struct tas *tas)
  650. {
  651. mutex_lock(&tas->mtx);
  652. tas->hw_enabled = 0;
  653. tas->acr |= TAS_ACR_ANALOG_PDOWN;
  654. tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
  655. mutex_unlock(&tas->mtx);
  656. return 0;
  657. }
  658. static int tas_resume(struct tas *tas)
  659. {
  660. /* reset codec */
  661. mutex_lock(&tas->mtx);
  662. tas_reset_init(tas);
  663. tas_set_volume(tas);
  664. tas_set_mixer(tas);
  665. tas->hw_enabled = 1;
  666. mutex_unlock(&tas->mtx);
  667. return 0;
  668. }
  669. static int _tas_suspend(struct codec_info_item *cii, pm_message_t state)
  670. {
  671. return tas_suspend(cii->codec_data);
  672. }
  673. static int _tas_resume(struct codec_info_item *cii)
  674. {
  675. return tas_resume(cii->codec_data);
  676. }
  677. #else /* CONFIG_PM */
  678. #define _tas_suspend NULL
  679. #define _tas_resume NULL
  680. #endif /* CONFIG_PM */
  681. static struct codec_info tas_codec_info = {
  682. .transfers = tas_transfers,
  683. /* in theory, we can drive it at 512 too...
  684. * but so far the framework doesn't allow
  685. * for that and I don't see much point in it. */
  686. .sysclock_factor = 256,
  687. /* same here, could be 32 for just one 16 bit format */
  688. .bus_factor = 64,
  689. .owner = THIS_MODULE,
  690. .usable = tas_usable,
  691. .switch_clock = tas_switch_clock,
  692. .suspend = _tas_suspend,
  693. .resume = _tas_resume,
  694. };
  695. static int tas_init_codec(struct aoa_codec *codec)
  696. {
  697. struct tas *tas = codec_to_tas(codec);
  698. int err;
  699. if (!tas->codec.gpio || !tas->codec.gpio->methods) {
  700. printk(KERN_ERR PFX "gpios not assigned!!\n");
  701. return -EINVAL;
  702. }
  703. mutex_lock(&tas->mtx);
  704. if (tas_reset_init(tas)) {
  705. printk(KERN_ERR PFX "tas failed to initialise\n");
  706. mutex_unlock(&tas->mtx);
  707. return -ENXIO;
  708. }
  709. tas->hw_enabled = 1;
  710. mutex_unlock(&tas->mtx);
  711. if (tas->codec.soundbus_dev->attach_codec(tas->codec.soundbus_dev,
  712. aoa_get_card(),
  713. &tas_codec_info, tas)) {
  714. printk(KERN_ERR PFX "error attaching tas to soundbus\n");
  715. return -ENODEV;
  716. }
  717. if (aoa_snd_device_new(SNDRV_DEV_LOWLEVEL, tas, &ops)) {
  718. printk(KERN_ERR PFX "failed to create tas snd device!\n");
  719. return -ENODEV;
  720. }
  721. err = aoa_snd_ctl_add(snd_ctl_new1(&volume_control, tas));
  722. if (err)
  723. goto error;
  724. err = aoa_snd_ctl_add(snd_ctl_new1(&mute_control, tas));
  725. if (err)
  726. goto error;
  727. err = aoa_snd_ctl_add(snd_ctl_new1(&pcm1_control, tas));
  728. if (err)
  729. goto error;
  730. err = aoa_snd_ctl_add(snd_ctl_new1(&monitor_control, tas));
  731. if (err)
  732. goto error;
  733. err = aoa_snd_ctl_add(snd_ctl_new1(&capture_source_control, tas));
  734. if (err)
  735. goto error;
  736. err = aoa_snd_ctl_add(snd_ctl_new1(&drc_range_control, tas));
  737. if (err)
  738. goto error;
  739. err = aoa_snd_ctl_add(snd_ctl_new1(&drc_switch_control, tas));
  740. if (err)
  741. goto error;
  742. err = aoa_snd_ctl_add(snd_ctl_new1(&treble_control, tas));
  743. if (err)
  744. goto error;
  745. err = aoa_snd_ctl_add(snd_ctl_new1(&bass_control, tas));
  746. if (err)
  747. goto error;
  748. return 0;
  749. error:
  750. tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
  751. snd_device_free(aoa_get_card(), tas);
  752. return err;
  753. }
  754. static void tas_exit_codec(struct aoa_codec *codec)
  755. {
  756. struct tas *tas = codec_to_tas(codec);
  757. if (!tas->codec.soundbus_dev)
  758. return;
  759. tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
  760. }
  761. static struct i2c_driver tas_driver;
  762. static int tas_create(struct i2c_adapter *adapter,
  763. struct device_node *node,
  764. int addr)
  765. {
  766. struct tas *tas;
  767. tas = kzalloc(sizeof(struct tas), GFP_KERNEL);
  768. if (!tas)
  769. return -ENOMEM;
  770. mutex_init(&tas->mtx);
  771. tas->i2c.driver = &tas_driver;
  772. tas->i2c.adapter = adapter;
  773. tas->i2c.addr = addr;
  774. /* seems that half is a saner default */
  775. tas->drc_range = TAS3004_DRC_MAX / 2;
  776. strlcpy(tas->i2c.name, "tas audio codec", I2C_NAME_SIZE);
  777. if (i2c_attach_client(&tas->i2c)) {
  778. printk(KERN_ERR PFX "failed to attach to i2c\n");
  779. goto fail;
  780. }
  781. strlcpy(tas->codec.name, "tas", MAX_CODEC_NAME_LEN);
  782. tas->codec.owner = THIS_MODULE;
  783. tas->codec.init = tas_init_codec;
  784. tas->codec.exit = tas_exit_codec;
  785. tas->codec.node = of_node_get(node);
  786. if (aoa_codec_register(&tas->codec)) {
  787. goto detach;
  788. }
  789. printk(KERN_DEBUG
  790. "snd-aoa-codec-tas: tas found, addr 0x%02x on %s\n",
  791. addr, node->full_name);
  792. return 0;
  793. detach:
  794. i2c_detach_client(&tas->i2c);
  795. fail:
  796. mutex_destroy(&tas->mtx);
  797. kfree(tas);
  798. return -EINVAL;
  799. }
  800. static int tas_i2c_attach(struct i2c_adapter *adapter)
  801. {
  802. struct device_node *busnode, *dev = NULL;
  803. struct pmac_i2c_bus *bus;
  804. bus = pmac_i2c_adapter_to_bus(adapter);
  805. if (bus == NULL)
  806. return -ENODEV;
  807. busnode = pmac_i2c_get_bus_node(bus);
  808. while ((dev = of_get_next_child(busnode, dev)) != NULL) {
  809. if (of_device_is_compatible(dev, "tas3004")) {
  810. const u32 *addr;
  811. printk(KERN_DEBUG PFX "found tas3004\n");
  812. addr = of_get_property(dev, "reg", NULL);
  813. if (!addr)
  814. continue;
  815. return tas_create(adapter, dev, ((*addr) >> 1) & 0x7f);
  816. }
  817. /* older machines have no 'codec' node with a 'compatible'
  818. * property that says 'tas3004', they just have a 'deq'
  819. * node without any such property... */
  820. if (strcmp(dev->name, "deq") == 0) {
  821. const u32 *_addr;
  822. u32 addr;
  823. printk(KERN_DEBUG PFX "found 'deq' node\n");
  824. _addr = of_get_property(dev, "i2c-address", NULL);
  825. if (!_addr)
  826. continue;
  827. addr = ((*_addr) >> 1) & 0x7f;
  828. /* now, if the address doesn't match any of the two
  829. * that a tas3004 can have, we cannot handle this.
  830. * I doubt it ever happens but hey. */
  831. if (addr != 0x34 && addr != 0x35)
  832. continue;
  833. return tas_create(adapter, dev, addr);
  834. }
  835. }
  836. return -ENODEV;
  837. }
  838. static int tas_i2c_detach(struct i2c_client *client)
  839. {
  840. struct tas *tas = container_of(client, struct tas, i2c);
  841. int err;
  842. u8 tmp = TAS_ACR_ANALOG_PDOWN;
  843. if ((err = i2c_detach_client(client)))
  844. return err;
  845. aoa_codec_unregister(&tas->codec);
  846. of_node_put(tas->codec.node);
  847. /* power down codec chip */
  848. tas_write_reg(tas, TAS_REG_ACR, 1, &tmp);
  849. mutex_destroy(&tas->mtx);
  850. kfree(tas);
  851. return 0;
  852. }
  853. static struct i2c_driver tas_driver = {
  854. .driver = {
  855. .name = "aoa_codec_tas",
  856. .owner = THIS_MODULE,
  857. },
  858. .attach_adapter = tas_i2c_attach,
  859. .detach_client = tas_i2c_detach,
  860. };
  861. static int __init tas_init(void)
  862. {
  863. return i2c_add_driver(&tas_driver);
  864. }
  865. static void __exit tas_exit(void)
  866. {
  867. i2c_del_driver(&tas_driver);
  868. }
  869. module_init(tas_init);
  870. module_exit(tas_exit);