snd-aoa-codec-tas.c 26 KB

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