tas.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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 char *texts[] = { "Line-In", "Microphone" };
  415. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  416. uinfo->count = 1;
  417. uinfo->value.enumerated.items = 2;
  418. if (uinfo->value.enumerated.item > 1)
  419. uinfo->value.enumerated.item = 1;
  420. strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  421. return 0;
  422. }
  423. static int tas_snd_capture_source_get(struct snd_kcontrol *kcontrol,
  424. struct snd_ctl_elem_value *ucontrol)
  425. {
  426. struct tas *tas = snd_kcontrol_chip(kcontrol);
  427. mutex_lock(&tas->mtx);
  428. ucontrol->value.enumerated.item[0] = !!(tas->acr & TAS_ACR_INPUT_B);
  429. mutex_unlock(&tas->mtx);
  430. return 0;
  431. }
  432. static int tas_snd_capture_source_put(struct snd_kcontrol *kcontrol,
  433. struct snd_ctl_elem_value *ucontrol)
  434. {
  435. struct tas *tas = snd_kcontrol_chip(kcontrol);
  436. int oldacr;
  437. if (ucontrol->value.enumerated.item[0] > 1)
  438. return -EINVAL;
  439. mutex_lock(&tas->mtx);
  440. oldacr = tas->acr;
  441. /*
  442. * Despite what the data sheet says in one place, the
  443. * TAS_ACR_B_MONAUREAL bit forces mono output even when
  444. * input A (line in) is selected.
  445. */
  446. tas->acr &= ~(TAS_ACR_INPUT_B | TAS_ACR_B_MONAUREAL);
  447. if (ucontrol->value.enumerated.item[0])
  448. tas->acr |= TAS_ACR_INPUT_B | TAS_ACR_B_MONAUREAL |
  449. TAS_ACR_B_MON_SEL_RIGHT;
  450. if (oldacr == tas->acr) {
  451. mutex_unlock(&tas->mtx);
  452. return 0;
  453. }
  454. if (tas->hw_enabled)
  455. tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
  456. mutex_unlock(&tas->mtx);
  457. return 1;
  458. }
  459. static struct snd_kcontrol_new capture_source_control = {
  460. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  461. /* If we name this 'Input Source', it properly shows up in
  462. * alsamixer as a selection, * but it's shown under the
  463. * 'Playback' category.
  464. * If I name it 'Capture Source', it shows up in strange
  465. * ways (two bools of which one can be selected at a
  466. * time) but at least it's shown in the 'Capture'
  467. * category.
  468. * I was told that this was due to backward compatibility,
  469. * but I don't understand then why the mangling is *not*
  470. * done when I name it "Input Source".....
  471. */
  472. .name = "Capture Source",
  473. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  474. .info = tas_snd_capture_source_info,
  475. .get = tas_snd_capture_source_get,
  476. .put = tas_snd_capture_source_put,
  477. };
  478. static int tas_snd_treble_info(struct snd_kcontrol *kcontrol,
  479. struct snd_ctl_elem_info *uinfo)
  480. {
  481. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  482. uinfo->count = 1;
  483. uinfo->value.integer.min = TAS3004_TREBLE_MIN;
  484. uinfo->value.integer.max = TAS3004_TREBLE_MAX;
  485. return 0;
  486. }
  487. static int tas_snd_treble_get(struct snd_kcontrol *kcontrol,
  488. struct snd_ctl_elem_value *ucontrol)
  489. {
  490. struct tas *tas = snd_kcontrol_chip(kcontrol);
  491. mutex_lock(&tas->mtx);
  492. ucontrol->value.integer.value[0] = tas->treble;
  493. mutex_unlock(&tas->mtx);
  494. return 0;
  495. }
  496. static int tas_snd_treble_put(struct snd_kcontrol *kcontrol,
  497. struct snd_ctl_elem_value *ucontrol)
  498. {
  499. struct tas *tas = snd_kcontrol_chip(kcontrol);
  500. if (ucontrol->value.integer.value[0] < TAS3004_TREBLE_MIN ||
  501. ucontrol->value.integer.value[0] > TAS3004_TREBLE_MAX)
  502. return -EINVAL;
  503. mutex_lock(&tas->mtx);
  504. if (tas->treble == ucontrol->value.integer.value[0]) {
  505. mutex_unlock(&tas->mtx);
  506. return 0;
  507. }
  508. tas->treble = ucontrol->value.integer.value[0];
  509. if (tas->hw_enabled)
  510. tas_set_treble(tas);
  511. mutex_unlock(&tas->mtx);
  512. return 1;
  513. }
  514. static struct snd_kcontrol_new treble_control = {
  515. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  516. .name = "Treble",
  517. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  518. .info = tas_snd_treble_info,
  519. .get = tas_snd_treble_get,
  520. .put = tas_snd_treble_put,
  521. };
  522. static int tas_snd_bass_info(struct snd_kcontrol *kcontrol,
  523. struct snd_ctl_elem_info *uinfo)
  524. {
  525. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  526. uinfo->count = 1;
  527. uinfo->value.integer.min = TAS3004_BASS_MIN;
  528. uinfo->value.integer.max = TAS3004_BASS_MAX;
  529. return 0;
  530. }
  531. static int tas_snd_bass_get(struct snd_kcontrol *kcontrol,
  532. struct snd_ctl_elem_value *ucontrol)
  533. {
  534. struct tas *tas = snd_kcontrol_chip(kcontrol);
  535. mutex_lock(&tas->mtx);
  536. ucontrol->value.integer.value[0] = tas->bass;
  537. mutex_unlock(&tas->mtx);
  538. return 0;
  539. }
  540. static int tas_snd_bass_put(struct snd_kcontrol *kcontrol,
  541. struct snd_ctl_elem_value *ucontrol)
  542. {
  543. struct tas *tas = snd_kcontrol_chip(kcontrol);
  544. if (ucontrol->value.integer.value[0] < TAS3004_BASS_MIN ||
  545. ucontrol->value.integer.value[0] > TAS3004_BASS_MAX)
  546. return -EINVAL;
  547. mutex_lock(&tas->mtx);
  548. if (tas->bass == ucontrol->value.integer.value[0]) {
  549. mutex_unlock(&tas->mtx);
  550. return 0;
  551. }
  552. tas->bass = ucontrol->value.integer.value[0];
  553. if (tas->hw_enabled)
  554. tas_set_bass(tas);
  555. mutex_unlock(&tas->mtx);
  556. return 1;
  557. }
  558. static struct snd_kcontrol_new bass_control = {
  559. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  560. .name = "Bass",
  561. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  562. .info = tas_snd_bass_info,
  563. .get = tas_snd_bass_get,
  564. .put = tas_snd_bass_put,
  565. };
  566. static struct transfer_info tas_transfers[] = {
  567. {
  568. /* input */
  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 = 1,
  572. },
  573. {
  574. /* output */
  575. .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S24_BE,
  576. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  577. .transfer_in = 0,
  578. },
  579. {}
  580. };
  581. static int tas_usable(struct codec_info_item *cii,
  582. struct transfer_info *ti,
  583. struct transfer_info *out)
  584. {
  585. return 1;
  586. }
  587. static int tas_reset_init(struct tas *tas)
  588. {
  589. u8 tmp;
  590. tas->codec.gpio->methods->all_amps_off(tas->codec.gpio);
  591. msleep(5);
  592. tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
  593. msleep(5);
  594. tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 1);
  595. msleep(20);
  596. tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
  597. msleep(10);
  598. tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio);
  599. tmp = TAS_MCS_SCLK64 | TAS_MCS_SPORT_MODE_I2S | TAS_MCS_SPORT_WL_24BIT;
  600. if (tas_write_reg(tas, TAS_REG_MCS, 1, &tmp))
  601. goto outerr;
  602. tas->acr |= TAS_ACR_ANALOG_PDOWN;
  603. if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr))
  604. goto outerr;
  605. tmp = 0;
  606. if (tas_write_reg(tas, TAS_REG_MCS2, 1, &tmp))
  607. goto outerr;
  608. tas3004_set_drc(tas);
  609. /* Set treble & bass to 0dB */
  610. tas->treble = TAS3004_TREBLE_ZERO;
  611. tas->bass = TAS3004_BASS_ZERO;
  612. tas_set_treble(tas);
  613. tas_set_bass(tas);
  614. tas->acr &= ~TAS_ACR_ANALOG_PDOWN;
  615. if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr))
  616. goto outerr;
  617. return 0;
  618. outerr:
  619. return -ENODEV;
  620. }
  621. static int tas_switch_clock(struct codec_info_item *cii, enum clock_switch clock)
  622. {
  623. struct tas *tas = cii->codec_data;
  624. switch(clock) {
  625. case CLOCK_SWITCH_PREPARE_SLAVE:
  626. /* Clocks are going away, mute mute mute */
  627. tas->codec.gpio->methods->all_amps_off(tas->codec.gpio);
  628. tas->hw_enabled = 0;
  629. break;
  630. case CLOCK_SWITCH_SLAVE:
  631. /* Clocks are back, re-init the codec */
  632. mutex_lock(&tas->mtx);
  633. tas_reset_init(tas);
  634. tas_set_volume(tas);
  635. tas_set_mixer(tas);
  636. tas->hw_enabled = 1;
  637. tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio);
  638. mutex_unlock(&tas->mtx);
  639. break;
  640. default:
  641. /* doesn't happen as of now */
  642. return -EINVAL;
  643. }
  644. return 0;
  645. }
  646. #ifdef CONFIG_PM
  647. /* we are controlled via i2c and assume that is always up
  648. * If that wasn't the case, we'd have to suspend once
  649. * our i2c device is suspended, and then take note of that! */
  650. static int tas_suspend(struct tas *tas)
  651. {
  652. mutex_lock(&tas->mtx);
  653. tas->hw_enabled = 0;
  654. tas->acr |= TAS_ACR_ANALOG_PDOWN;
  655. tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
  656. mutex_unlock(&tas->mtx);
  657. return 0;
  658. }
  659. static int tas_resume(struct tas *tas)
  660. {
  661. /* reset codec */
  662. mutex_lock(&tas->mtx);
  663. tas_reset_init(tas);
  664. tas_set_volume(tas);
  665. tas_set_mixer(tas);
  666. tas->hw_enabled = 1;
  667. mutex_unlock(&tas->mtx);
  668. return 0;
  669. }
  670. static int _tas_suspend(struct codec_info_item *cii, pm_message_t state)
  671. {
  672. return tas_suspend(cii->codec_data);
  673. }
  674. static int _tas_resume(struct codec_info_item *cii)
  675. {
  676. return tas_resume(cii->codec_data);
  677. }
  678. #else /* CONFIG_PM */
  679. #define _tas_suspend NULL
  680. #define _tas_resume NULL
  681. #endif /* CONFIG_PM */
  682. static struct codec_info tas_codec_info = {
  683. .transfers = tas_transfers,
  684. /* in theory, we can drive it at 512 too...
  685. * but so far the framework doesn't allow
  686. * for that and I don't see much point in it. */
  687. .sysclock_factor = 256,
  688. /* same here, could be 32 for just one 16 bit format */
  689. .bus_factor = 64,
  690. .owner = THIS_MODULE,
  691. .usable = tas_usable,
  692. .switch_clock = tas_switch_clock,
  693. .suspend = _tas_suspend,
  694. .resume = _tas_resume,
  695. };
  696. static int tas_init_codec(struct aoa_codec *codec)
  697. {
  698. struct tas *tas = codec_to_tas(codec);
  699. int err;
  700. if (!tas->codec.gpio || !tas->codec.gpio->methods) {
  701. printk(KERN_ERR PFX "gpios not assigned!!\n");
  702. return -EINVAL;
  703. }
  704. mutex_lock(&tas->mtx);
  705. if (tas_reset_init(tas)) {
  706. printk(KERN_ERR PFX "tas failed to initialise\n");
  707. mutex_unlock(&tas->mtx);
  708. return -ENXIO;
  709. }
  710. tas->hw_enabled = 1;
  711. mutex_unlock(&tas->mtx);
  712. if (tas->codec.soundbus_dev->attach_codec(tas->codec.soundbus_dev,
  713. aoa_get_card(),
  714. &tas_codec_info, tas)) {
  715. printk(KERN_ERR PFX "error attaching tas to soundbus\n");
  716. return -ENODEV;
  717. }
  718. if (aoa_snd_device_new(SNDRV_DEV_LOWLEVEL, tas, &ops)) {
  719. printk(KERN_ERR PFX "failed to create tas snd device!\n");
  720. return -ENODEV;
  721. }
  722. err = aoa_snd_ctl_add(snd_ctl_new1(&volume_control, tas));
  723. if (err)
  724. goto error;
  725. err = aoa_snd_ctl_add(snd_ctl_new1(&mute_control, tas));
  726. if (err)
  727. goto error;
  728. err = aoa_snd_ctl_add(snd_ctl_new1(&pcm1_control, tas));
  729. if (err)
  730. goto error;
  731. err = aoa_snd_ctl_add(snd_ctl_new1(&monitor_control, tas));
  732. if (err)
  733. goto error;
  734. err = aoa_snd_ctl_add(snd_ctl_new1(&capture_source_control, tas));
  735. if (err)
  736. goto error;
  737. err = aoa_snd_ctl_add(snd_ctl_new1(&drc_range_control, tas));
  738. if (err)
  739. goto error;
  740. err = aoa_snd_ctl_add(snd_ctl_new1(&drc_switch_control, tas));
  741. if (err)
  742. goto error;
  743. err = aoa_snd_ctl_add(snd_ctl_new1(&treble_control, tas));
  744. if (err)
  745. goto error;
  746. err = aoa_snd_ctl_add(snd_ctl_new1(&bass_control, tas));
  747. if (err)
  748. goto error;
  749. return 0;
  750. error:
  751. tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
  752. snd_device_free(aoa_get_card(), tas);
  753. return err;
  754. }
  755. static void tas_exit_codec(struct aoa_codec *codec)
  756. {
  757. struct tas *tas = codec_to_tas(codec);
  758. if (!tas->codec.soundbus_dev)
  759. return;
  760. tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
  761. }
  762. static int tas_create(struct i2c_adapter *adapter,
  763. struct device_node *node,
  764. int addr)
  765. {
  766. struct i2c_board_info info;
  767. struct i2c_client *client;
  768. memset(&info, 0, sizeof(struct i2c_board_info));
  769. strlcpy(info.type, "aoa_codec_tas", I2C_NAME_SIZE);
  770. info.addr = addr;
  771. info.platform_data = node;
  772. client = i2c_new_device(adapter, &info);
  773. if (!client)
  774. return -ENODEV;
  775. /*
  776. * We know the driver is already loaded, so the device should be
  777. * already bound. If not it means binding failed, and then there
  778. * is no point in keeping the device instantiated.
  779. */
  780. if (!client->driver) {
  781. i2c_unregister_device(client);
  782. return -ENODEV;
  783. }
  784. /*
  785. * Let i2c-core delete that device on driver removal.
  786. * This is safe because i2c-core holds the core_lock mutex for us.
  787. */
  788. list_add_tail(&client->detected, &client->driver->clients);
  789. return 0;
  790. }
  791. static int tas_i2c_probe(struct i2c_client *client,
  792. const struct i2c_device_id *id)
  793. {
  794. struct device_node *node = client->dev.platform_data;
  795. struct tas *tas;
  796. tas = kzalloc(sizeof(struct tas), GFP_KERNEL);
  797. if (!tas)
  798. return -ENOMEM;
  799. mutex_init(&tas->mtx);
  800. tas->i2c = client;
  801. i2c_set_clientdata(client, tas);
  802. /* seems that half is a saner default */
  803. tas->drc_range = TAS3004_DRC_MAX / 2;
  804. strlcpy(tas->codec.name, "tas", MAX_CODEC_NAME_LEN);
  805. tas->codec.owner = THIS_MODULE;
  806. tas->codec.init = tas_init_codec;
  807. tas->codec.exit = tas_exit_codec;
  808. tas->codec.node = of_node_get(node);
  809. if (aoa_codec_register(&tas->codec)) {
  810. goto fail;
  811. }
  812. printk(KERN_DEBUG
  813. "snd-aoa-codec-tas: tas found, addr 0x%02x on %s\n",
  814. (unsigned int)client->addr, node->full_name);
  815. return 0;
  816. fail:
  817. mutex_destroy(&tas->mtx);
  818. kfree(tas);
  819. return -EINVAL;
  820. }
  821. static int tas_i2c_attach(struct i2c_adapter *adapter)
  822. {
  823. struct device_node *busnode, *dev = NULL;
  824. struct pmac_i2c_bus *bus;
  825. bus = pmac_i2c_adapter_to_bus(adapter);
  826. if (bus == NULL)
  827. return -ENODEV;
  828. busnode = pmac_i2c_get_bus_node(bus);
  829. while ((dev = of_get_next_child(busnode, dev)) != NULL) {
  830. if (of_device_is_compatible(dev, "tas3004")) {
  831. const u32 *addr;
  832. printk(KERN_DEBUG PFX "found tas3004\n");
  833. addr = of_get_property(dev, "reg", NULL);
  834. if (!addr)
  835. continue;
  836. return tas_create(adapter, dev, ((*addr) >> 1) & 0x7f);
  837. }
  838. /* older machines have no 'codec' node with a 'compatible'
  839. * property that says 'tas3004', they just have a 'deq'
  840. * node without any such property... */
  841. if (strcmp(dev->name, "deq") == 0) {
  842. const u32 *_addr;
  843. u32 addr;
  844. printk(KERN_DEBUG PFX "found 'deq' node\n");
  845. _addr = of_get_property(dev, "i2c-address", NULL);
  846. if (!_addr)
  847. continue;
  848. addr = ((*_addr) >> 1) & 0x7f;
  849. /* now, if the address doesn't match any of the two
  850. * that a tas3004 can have, we cannot handle this.
  851. * I doubt it ever happens but hey. */
  852. if (addr != 0x34 && addr != 0x35)
  853. continue;
  854. return tas_create(adapter, dev, addr);
  855. }
  856. }
  857. return -ENODEV;
  858. }
  859. static int tas_i2c_remove(struct i2c_client *client)
  860. {
  861. struct tas *tas = i2c_get_clientdata(client);
  862. u8 tmp = TAS_ACR_ANALOG_PDOWN;
  863. aoa_codec_unregister(&tas->codec);
  864. of_node_put(tas->codec.node);
  865. /* power down codec chip */
  866. tas_write_reg(tas, TAS_REG_ACR, 1, &tmp);
  867. mutex_destroy(&tas->mtx);
  868. kfree(tas);
  869. return 0;
  870. }
  871. static const struct i2c_device_id tas_i2c_id[] = {
  872. { "aoa_codec_tas", 0 },
  873. { }
  874. };
  875. static struct i2c_driver tas_driver = {
  876. .driver = {
  877. .name = "aoa_codec_tas",
  878. .owner = THIS_MODULE,
  879. },
  880. .attach_adapter = tas_i2c_attach,
  881. .probe = tas_i2c_probe,
  882. .remove = tas_i2c_remove,
  883. .id_table = tas_i2c_id,
  884. };
  885. module_i2c_driver(tas_driver);