snd-aoa-codec-tas.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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. mutex_lock(&tas->mtx);
  221. if (tas->cached_volume_l == ucontrol->value.integer.value[0]
  222. && tas->cached_volume_r == ucontrol->value.integer.value[1]) {
  223. mutex_unlock(&tas->mtx);
  224. return 0;
  225. }
  226. tas->cached_volume_l = ucontrol->value.integer.value[0];
  227. tas->cached_volume_r = ucontrol->value.integer.value[1];
  228. if (tas->hw_enabled)
  229. tas_set_volume(tas);
  230. mutex_unlock(&tas->mtx);
  231. return 1;
  232. }
  233. static struct snd_kcontrol_new volume_control = {
  234. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  235. .name = "Master Playback Volume",
  236. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  237. .info = tas_snd_vol_info,
  238. .get = tas_snd_vol_get,
  239. .put = tas_snd_vol_put,
  240. };
  241. static int tas_snd_mute_info(struct snd_kcontrol *kcontrol,
  242. struct snd_ctl_elem_info *uinfo)
  243. {
  244. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  245. uinfo->count = 2;
  246. uinfo->value.integer.min = 0;
  247. uinfo->value.integer.max = 1;
  248. return 0;
  249. }
  250. static int tas_snd_mute_get(struct snd_kcontrol *kcontrol,
  251. struct snd_ctl_elem_value *ucontrol)
  252. {
  253. struct tas *tas = snd_kcontrol_chip(kcontrol);
  254. mutex_lock(&tas->mtx);
  255. ucontrol->value.integer.value[0] = !tas->mute_l;
  256. ucontrol->value.integer.value[1] = !tas->mute_r;
  257. mutex_unlock(&tas->mtx);
  258. return 0;
  259. }
  260. static int tas_snd_mute_put(struct snd_kcontrol *kcontrol,
  261. struct snd_ctl_elem_value *ucontrol)
  262. {
  263. struct tas *tas = snd_kcontrol_chip(kcontrol);
  264. mutex_lock(&tas->mtx);
  265. if (tas->mute_l == !ucontrol->value.integer.value[0]
  266. && tas->mute_r == !ucontrol->value.integer.value[1]) {
  267. mutex_unlock(&tas->mtx);
  268. return 0;
  269. }
  270. tas->mute_l = !ucontrol->value.integer.value[0];
  271. tas->mute_r = !ucontrol->value.integer.value[1];
  272. if (tas->hw_enabled)
  273. tas_set_volume(tas);
  274. mutex_unlock(&tas->mtx);
  275. return 1;
  276. }
  277. static struct snd_kcontrol_new mute_control = {
  278. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  279. .name = "Master Playback Switch",
  280. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  281. .info = tas_snd_mute_info,
  282. .get = tas_snd_mute_get,
  283. .put = tas_snd_mute_put,
  284. };
  285. static int tas_snd_mixer_info(struct snd_kcontrol *kcontrol,
  286. struct snd_ctl_elem_info *uinfo)
  287. {
  288. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  289. uinfo->count = 2;
  290. uinfo->value.integer.min = 0;
  291. uinfo->value.integer.max = 177;
  292. return 0;
  293. }
  294. static int tas_snd_mixer_get(struct snd_kcontrol *kcontrol,
  295. struct snd_ctl_elem_value *ucontrol)
  296. {
  297. struct tas *tas = snd_kcontrol_chip(kcontrol);
  298. int idx = kcontrol->private_value;
  299. mutex_lock(&tas->mtx);
  300. ucontrol->value.integer.value[0] = tas->mixer_l[idx];
  301. ucontrol->value.integer.value[1] = tas->mixer_r[idx];
  302. mutex_unlock(&tas->mtx);
  303. return 0;
  304. }
  305. static int tas_snd_mixer_put(struct snd_kcontrol *kcontrol,
  306. struct snd_ctl_elem_value *ucontrol)
  307. {
  308. struct tas *tas = snd_kcontrol_chip(kcontrol);
  309. int idx = kcontrol->private_value;
  310. mutex_lock(&tas->mtx);
  311. if (tas->mixer_l[idx] == ucontrol->value.integer.value[0]
  312. && tas->mixer_r[idx] == ucontrol->value.integer.value[1]) {
  313. mutex_unlock(&tas->mtx);
  314. return 0;
  315. }
  316. tas->mixer_l[idx] = ucontrol->value.integer.value[0];
  317. tas->mixer_r[idx] = ucontrol->value.integer.value[1];
  318. if (tas->hw_enabled)
  319. tas_set_mixer(tas);
  320. mutex_unlock(&tas->mtx);
  321. return 1;
  322. }
  323. #define MIXER_CONTROL(n,descr,idx) \
  324. static struct snd_kcontrol_new n##_control = { \
  325. .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  326. .name = descr " Playback Volume", \
  327. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
  328. .info = tas_snd_mixer_info, \
  329. .get = tas_snd_mixer_get, \
  330. .put = tas_snd_mixer_put, \
  331. .private_value = idx, \
  332. }
  333. MIXER_CONTROL(pcm1, "PCM", 0);
  334. MIXER_CONTROL(monitor, "Monitor", 2);
  335. static int tas_snd_drc_range_info(struct snd_kcontrol *kcontrol,
  336. struct snd_ctl_elem_info *uinfo)
  337. {
  338. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  339. uinfo->count = 1;
  340. uinfo->value.integer.min = 0;
  341. uinfo->value.integer.max = TAS3004_DRC_MAX;
  342. return 0;
  343. }
  344. static int tas_snd_drc_range_get(struct snd_kcontrol *kcontrol,
  345. struct snd_ctl_elem_value *ucontrol)
  346. {
  347. struct tas *tas = snd_kcontrol_chip(kcontrol);
  348. mutex_lock(&tas->mtx);
  349. ucontrol->value.integer.value[0] = tas->drc_range;
  350. mutex_unlock(&tas->mtx);
  351. return 0;
  352. }
  353. static int tas_snd_drc_range_put(struct snd_kcontrol *kcontrol,
  354. struct snd_ctl_elem_value *ucontrol)
  355. {
  356. struct tas *tas = snd_kcontrol_chip(kcontrol);
  357. mutex_lock(&tas->mtx);
  358. if (tas->drc_range == ucontrol->value.integer.value[0]) {
  359. mutex_unlock(&tas->mtx);
  360. return 0;
  361. }
  362. tas->drc_range = ucontrol->value.integer.value[0];
  363. if (tas->hw_enabled)
  364. tas3004_set_drc(tas);
  365. mutex_unlock(&tas->mtx);
  366. return 1;
  367. }
  368. static struct snd_kcontrol_new drc_range_control = {
  369. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  370. .name = "DRC Range",
  371. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  372. .info = tas_snd_drc_range_info,
  373. .get = tas_snd_drc_range_get,
  374. .put = tas_snd_drc_range_put,
  375. };
  376. static int tas_snd_drc_switch_info(struct snd_kcontrol *kcontrol,
  377. struct snd_ctl_elem_info *uinfo)
  378. {
  379. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  380. uinfo->count = 1;
  381. uinfo->value.integer.min = 0;
  382. uinfo->value.integer.max = 1;
  383. return 0;
  384. }
  385. static int tas_snd_drc_switch_get(struct snd_kcontrol *kcontrol,
  386. struct snd_ctl_elem_value *ucontrol)
  387. {
  388. struct tas *tas = snd_kcontrol_chip(kcontrol);
  389. mutex_lock(&tas->mtx);
  390. ucontrol->value.integer.value[0] = tas->drc_enabled;
  391. mutex_unlock(&tas->mtx);
  392. return 0;
  393. }
  394. static int tas_snd_drc_switch_put(struct snd_kcontrol *kcontrol,
  395. struct snd_ctl_elem_value *ucontrol)
  396. {
  397. struct tas *tas = snd_kcontrol_chip(kcontrol);
  398. mutex_lock(&tas->mtx);
  399. if (tas->drc_enabled == ucontrol->value.integer.value[0]) {
  400. mutex_unlock(&tas->mtx);
  401. return 0;
  402. }
  403. tas->drc_enabled = ucontrol->value.integer.value[0];
  404. if (tas->hw_enabled)
  405. tas3004_set_drc(tas);
  406. mutex_unlock(&tas->mtx);
  407. return 1;
  408. }
  409. static struct snd_kcontrol_new drc_switch_control = {
  410. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  411. .name = "DRC Range Switch",
  412. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  413. .info = tas_snd_drc_switch_info,
  414. .get = tas_snd_drc_switch_get,
  415. .put = tas_snd_drc_switch_put,
  416. };
  417. static int tas_snd_capture_source_info(struct snd_kcontrol *kcontrol,
  418. struct snd_ctl_elem_info *uinfo)
  419. {
  420. static char *texts[] = { "Line-In", "Microphone" };
  421. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  422. uinfo->count = 1;
  423. uinfo->value.enumerated.items = 2;
  424. if (uinfo->value.enumerated.item > 1)
  425. uinfo->value.enumerated.item = 1;
  426. strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  427. return 0;
  428. }
  429. static int tas_snd_capture_source_get(struct snd_kcontrol *kcontrol,
  430. struct snd_ctl_elem_value *ucontrol)
  431. {
  432. struct tas *tas = snd_kcontrol_chip(kcontrol);
  433. mutex_lock(&tas->mtx);
  434. ucontrol->value.enumerated.item[0] = !!(tas->acr & TAS_ACR_INPUT_B);
  435. mutex_unlock(&tas->mtx);
  436. return 0;
  437. }
  438. static int tas_snd_capture_source_put(struct snd_kcontrol *kcontrol,
  439. struct snd_ctl_elem_value *ucontrol)
  440. {
  441. struct tas *tas = snd_kcontrol_chip(kcontrol);
  442. int oldacr;
  443. mutex_lock(&tas->mtx);
  444. oldacr = tas->acr;
  445. /*
  446. * Despite what the data sheet says in one place, the
  447. * TAS_ACR_B_MONAUREAL bit forces mono output even when
  448. * input A (line in) is selected.
  449. */
  450. tas->acr &= ~(TAS_ACR_INPUT_B | TAS_ACR_B_MONAUREAL);
  451. if (ucontrol->value.enumerated.item[0])
  452. tas->acr |= TAS_ACR_INPUT_B | TAS_ACR_B_MONAUREAL |
  453. TAS_ACR_B_MON_SEL_RIGHT;
  454. if (oldacr == tas->acr) {
  455. mutex_unlock(&tas->mtx);
  456. return 0;
  457. }
  458. if (tas->hw_enabled)
  459. tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
  460. mutex_unlock(&tas->mtx);
  461. return 1;
  462. }
  463. static struct snd_kcontrol_new capture_source_control = {
  464. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  465. /* If we name this 'Input Source', it properly shows up in
  466. * alsamixer as a selection, * but it's shown under the
  467. * 'Playback' category.
  468. * If I name it 'Capture Source', it shows up in strange
  469. * ways (two bools of which one can be selected at a
  470. * time) but at least it's shown in the 'Capture'
  471. * category.
  472. * I was told that this was due to backward compatibility,
  473. * but I don't understand then why the mangling is *not*
  474. * done when I name it "Input Source".....
  475. */
  476. .name = "Capture Source",
  477. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  478. .info = tas_snd_capture_source_info,
  479. .get = tas_snd_capture_source_get,
  480. .put = tas_snd_capture_source_put,
  481. };
  482. static int tas_snd_treble_info(struct snd_kcontrol *kcontrol,
  483. struct snd_ctl_elem_info *uinfo)
  484. {
  485. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  486. uinfo->count = 1;
  487. uinfo->value.integer.min = TAS3004_TREBLE_MIN;
  488. uinfo->value.integer.max = TAS3004_TREBLE_MAX;
  489. return 0;
  490. }
  491. static int tas_snd_treble_get(struct snd_kcontrol *kcontrol,
  492. struct snd_ctl_elem_value *ucontrol)
  493. {
  494. struct tas *tas = snd_kcontrol_chip(kcontrol);
  495. mutex_lock(&tas->mtx);
  496. ucontrol->value.integer.value[0] = tas->treble;
  497. mutex_unlock(&tas->mtx);
  498. return 0;
  499. }
  500. static int tas_snd_treble_put(struct snd_kcontrol *kcontrol,
  501. struct snd_ctl_elem_value *ucontrol)
  502. {
  503. struct tas *tas = snd_kcontrol_chip(kcontrol);
  504. mutex_lock(&tas->mtx);
  505. if (tas->treble == ucontrol->value.integer.value[0]) {
  506. mutex_unlock(&tas->mtx);
  507. return 0;
  508. }
  509. tas->treble = ucontrol->value.integer.value[0];
  510. if (tas->hw_enabled)
  511. tas_set_treble(tas);
  512. mutex_unlock(&tas->mtx);
  513. return 1;
  514. }
  515. static struct snd_kcontrol_new treble_control = {
  516. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  517. .name = "Treble",
  518. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  519. .info = tas_snd_treble_info,
  520. .get = tas_snd_treble_get,
  521. .put = tas_snd_treble_put,
  522. };
  523. static int tas_snd_bass_info(struct snd_kcontrol *kcontrol,
  524. struct snd_ctl_elem_info *uinfo)
  525. {
  526. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  527. uinfo->count = 1;
  528. uinfo->value.integer.min = TAS3004_BASS_MIN;
  529. uinfo->value.integer.max = TAS3004_BASS_MAX;
  530. return 0;
  531. }
  532. static int tas_snd_bass_get(struct snd_kcontrol *kcontrol,
  533. struct snd_ctl_elem_value *ucontrol)
  534. {
  535. struct tas *tas = snd_kcontrol_chip(kcontrol);
  536. mutex_lock(&tas->mtx);
  537. ucontrol->value.integer.value[0] = tas->bass;
  538. mutex_unlock(&tas->mtx);
  539. return 0;
  540. }
  541. static int tas_snd_bass_put(struct snd_kcontrol *kcontrol,
  542. struct snd_ctl_elem_value *ucontrol)
  543. {
  544. struct tas *tas = snd_kcontrol_chip(kcontrol);
  545. mutex_lock(&tas->mtx);
  546. if (tas->bass == ucontrol->value.integer.value[0]) {
  547. mutex_unlock(&tas->mtx);
  548. return 0;
  549. }
  550. tas->bass = ucontrol->value.integer.value[0];
  551. if (tas->hw_enabled)
  552. tas_set_bass(tas);
  553. mutex_unlock(&tas->mtx);
  554. return 1;
  555. }
  556. static struct snd_kcontrol_new bass_control = {
  557. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  558. .name = "Bass",
  559. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  560. .info = tas_snd_bass_info,
  561. .get = tas_snd_bass_get,
  562. .put = tas_snd_bass_put,
  563. };
  564. static struct transfer_info tas_transfers[] = {
  565. {
  566. /* input */
  567. .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_BE |
  568. SNDRV_PCM_FMTBIT_S24_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_S16_BE |
  575. SNDRV_PCM_FMTBIT_S24_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 struct i2c_driver tas_driver;
  763. static int tas_create(struct i2c_adapter *adapter,
  764. struct device_node *node,
  765. int addr)
  766. {
  767. struct tas *tas;
  768. tas = kzalloc(sizeof(struct tas), GFP_KERNEL);
  769. if (!tas)
  770. return -ENOMEM;
  771. mutex_init(&tas->mtx);
  772. tas->i2c.driver = &tas_driver;
  773. tas->i2c.adapter = adapter;
  774. tas->i2c.addr = addr;
  775. /* seems that half is a saner default */
  776. tas->drc_range = TAS3004_DRC_MAX / 2;
  777. strlcpy(tas->i2c.name, "tas audio codec", I2C_NAME_SIZE);
  778. if (i2c_attach_client(&tas->i2c)) {
  779. printk(KERN_ERR PFX "failed to attach to i2c\n");
  780. goto fail;
  781. }
  782. strlcpy(tas->codec.name, "tas", MAX_CODEC_NAME_LEN);
  783. tas->codec.owner = THIS_MODULE;
  784. tas->codec.init = tas_init_codec;
  785. tas->codec.exit = tas_exit_codec;
  786. tas->codec.node = of_node_get(node);
  787. if (aoa_codec_register(&tas->codec)) {
  788. goto detach;
  789. }
  790. printk(KERN_DEBUG
  791. "snd-aoa-codec-tas: tas found, addr 0x%02x on %s\n",
  792. addr, node->full_name);
  793. return 0;
  794. detach:
  795. i2c_detach_client(&tas->i2c);
  796. fail:
  797. mutex_destroy(&tas->mtx);
  798. kfree(tas);
  799. return -EINVAL;
  800. }
  801. static int tas_i2c_attach(struct i2c_adapter *adapter)
  802. {
  803. struct device_node *busnode, *dev = NULL;
  804. struct pmac_i2c_bus *bus;
  805. bus = pmac_i2c_adapter_to_bus(adapter);
  806. if (bus == NULL)
  807. return -ENODEV;
  808. busnode = pmac_i2c_get_bus_node(bus);
  809. while ((dev = of_get_next_child(busnode, dev)) != NULL) {
  810. if (of_device_is_compatible(dev, "tas3004")) {
  811. const u32 *addr;
  812. printk(KERN_DEBUG PFX "found tas3004\n");
  813. addr = of_get_property(dev, "reg", NULL);
  814. if (!addr)
  815. continue;
  816. return tas_create(adapter, dev, ((*addr) >> 1) & 0x7f);
  817. }
  818. /* older machines have no 'codec' node with a 'compatible'
  819. * property that says 'tas3004', they just have a 'deq'
  820. * node without any such property... */
  821. if (strcmp(dev->name, "deq") == 0) {
  822. const u32 *_addr;
  823. u32 addr;
  824. printk(KERN_DEBUG PFX "found 'deq' node\n");
  825. _addr = of_get_property(dev, "i2c-address", NULL);
  826. if (!_addr)
  827. continue;
  828. addr = ((*_addr) >> 1) & 0x7f;
  829. /* now, if the address doesn't match any of the two
  830. * that a tas3004 can have, we cannot handle this.
  831. * I doubt it ever happens but hey. */
  832. if (addr != 0x34 && addr != 0x35)
  833. continue;
  834. return tas_create(adapter, dev, addr);
  835. }
  836. }
  837. return -ENODEV;
  838. }
  839. static int tas_i2c_detach(struct i2c_client *client)
  840. {
  841. struct tas *tas = container_of(client, struct tas, i2c);
  842. int err;
  843. u8 tmp = TAS_ACR_ANALOG_PDOWN;
  844. if ((err = i2c_detach_client(client)))
  845. return err;
  846. aoa_codec_unregister(&tas->codec);
  847. of_node_put(tas->codec.node);
  848. /* power down codec chip */
  849. tas_write_reg(tas, TAS_REG_ACR, 1, &tmp);
  850. mutex_destroy(&tas->mtx);
  851. kfree(tas);
  852. return 0;
  853. }
  854. static struct i2c_driver tas_driver = {
  855. .driver = {
  856. .name = "aoa_codec_tas",
  857. .owner = THIS_MODULE,
  858. },
  859. .attach_adapter = tas_i2c_attach,
  860. .detach_client = tas_i2c_detach,
  861. };
  862. static int __init tas_init(void)
  863. {
  864. return i2c_add_driver(&tas_driver);
  865. }
  866. static void __exit tas_exit(void)
  867. {
  868. i2c_del_driver(&tas_driver);
  869. }
  870. module_init(tas_init);
  871. module_exit(tas_exit);