snd-aoa-codec-tas.c 24 KB

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