snd-aoa-codec-tas.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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. #define tas_snd_mute_info snd_ctl_boolean_stereo_info
  242. static int tas_snd_mute_get(struct snd_kcontrol *kcontrol,
  243. struct snd_ctl_elem_value *ucontrol)
  244. {
  245. struct tas *tas = snd_kcontrol_chip(kcontrol);
  246. mutex_lock(&tas->mtx);
  247. ucontrol->value.integer.value[0] = !tas->mute_l;
  248. ucontrol->value.integer.value[1] = !tas->mute_r;
  249. mutex_unlock(&tas->mtx);
  250. return 0;
  251. }
  252. static int tas_snd_mute_put(struct snd_kcontrol *kcontrol,
  253. struct snd_ctl_elem_value *ucontrol)
  254. {
  255. struct tas *tas = snd_kcontrol_chip(kcontrol);
  256. mutex_lock(&tas->mtx);
  257. if (tas->mute_l == !ucontrol->value.integer.value[0]
  258. && tas->mute_r == !ucontrol->value.integer.value[1]) {
  259. mutex_unlock(&tas->mtx);
  260. return 0;
  261. }
  262. tas->mute_l = !ucontrol->value.integer.value[0];
  263. tas->mute_r = !ucontrol->value.integer.value[1];
  264. if (tas->hw_enabled)
  265. tas_set_volume(tas);
  266. mutex_unlock(&tas->mtx);
  267. return 1;
  268. }
  269. static struct snd_kcontrol_new mute_control = {
  270. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  271. .name = "Master Playback Switch",
  272. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  273. .info = tas_snd_mute_info,
  274. .get = tas_snd_mute_get,
  275. .put = tas_snd_mute_put,
  276. };
  277. static int tas_snd_mixer_info(struct snd_kcontrol *kcontrol,
  278. struct snd_ctl_elem_info *uinfo)
  279. {
  280. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  281. uinfo->count = 2;
  282. uinfo->value.integer.min = 0;
  283. uinfo->value.integer.max = 177;
  284. return 0;
  285. }
  286. static int tas_snd_mixer_get(struct snd_kcontrol *kcontrol,
  287. struct snd_ctl_elem_value *ucontrol)
  288. {
  289. struct tas *tas = snd_kcontrol_chip(kcontrol);
  290. int idx = kcontrol->private_value;
  291. mutex_lock(&tas->mtx);
  292. ucontrol->value.integer.value[0] = tas->mixer_l[idx];
  293. ucontrol->value.integer.value[1] = tas->mixer_r[idx];
  294. mutex_unlock(&tas->mtx);
  295. return 0;
  296. }
  297. static int tas_snd_mixer_put(struct snd_kcontrol *kcontrol,
  298. struct snd_ctl_elem_value *ucontrol)
  299. {
  300. struct tas *tas = snd_kcontrol_chip(kcontrol);
  301. int idx = kcontrol->private_value;
  302. mutex_lock(&tas->mtx);
  303. if (tas->mixer_l[idx] == ucontrol->value.integer.value[0]
  304. && tas->mixer_r[idx] == ucontrol->value.integer.value[1]) {
  305. mutex_unlock(&tas->mtx);
  306. return 0;
  307. }
  308. tas->mixer_l[idx] = ucontrol->value.integer.value[0];
  309. tas->mixer_r[idx] = ucontrol->value.integer.value[1];
  310. if (tas->hw_enabled)
  311. tas_set_mixer(tas);
  312. mutex_unlock(&tas->mtx);
  313. return 1;
  314. }
  315. #define MIXER_CONTROL(n,descr,idx) \
  316. static struct snd_kcontrol_new n##_control = { \
  317. .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  318. .name = descr " Playback Volume", \
  319. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
  320. .info = tas_snd_mixer_info, \
  321. .get = tas_snd_mixer_get, \
  322. .put = tas_snd_mixer_put, \
  323. .private_value = idx, \
  324. }
  325. MIXER_CONTROL(pcm1, "PCM", 0);
  326. MIXER_CONTROL(monitor, "Monitor", 2);
  327. static int tas_snd_drc_range_info(struct snd_kcontrol *kcontrol,
  328. struct snd_ctl_elem_info *uinfo)
  329. {
  330. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  331. uinfo->count = 1;
  332. uinfo->value.integer.min = 0;
  333. uinfo->value.integer.max = TAS3004_DRC_MAX;
  334. return 0;
  335. }
  336. static int tas_snd_drc_range_get(struct snd_kcontrol *kcontrol,
  337. struct snd_ctl_elem_value *ucontrol)
  338. {
  339. struct tas *tas = snd_kcontrol_chip(kcontrol);
  340. mutex_lock(&tas->mtx);
  341. ucontrol->value.integer.value[0] = tas->drc_range;
  342. mutex_unlock(&tas->mtx);
  343. return 0;
  344. }
  345. static int tas_snd_drc_range_put(struct snd_kcontrol *kcontrol,
  346. struct snd_ctl_elem_value *ucontrol)
  347. {
  348. struct tas *tas = snd_kcontrol_chip(kcontrol);
  349. mutex_lock(&tas->mtx);
  350. if (tas->drc_range == ucontrol->value.integer.value[0]) {
  351. mutex_unlock(&tas->mtx);
  352. return 0;
  353. }
  354. tas->drc_range = ucontrol->value.integer.value[0];
  355. if (tas->hw_enabled)
  356. tas3004_set_drc(tas);
  357. mutex_unlock(&tas->mtx);
  358. return 1;
  359. }
  360. static struct snd_kcontrol_new drc_range_control = {
  361. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  362. .name = "DRC Range",
  363. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  364. .info = tas_snd_drc_range_info,
  365. .get = tas_snd_drc_range_get,
  366. .put = tas_snd_drc_range_put,
  367. };
  368. #define tas_snd_drc_switch_info snd_ctl_boolean_mono_info
  369. static int tas_snd_drc_switch_get(struct snd_kcontrol *kcontrol,
  370. struct snd_ctl_elem_value *ucontrol)
  371. {
  372. struct tas *tas = snd_kcontrol_chip(kcontrol);
  373. mutex_lock(&tas->mtx);
  374. ucontrol->value.integer.value[0] = tas->drc_enabled;
  375. mutex_unlock(&tas->mtx);
  376. return 0;
  377. }
  378. static int tas_snd_drc_switch_put(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. if (tas->drc_enabled == ucontrol->value.integer.value[0]) {
  384. mutex_unlock(&tas->mtx);
  385. return 0;
  386. }
  387. tas->drc_enabled = ucontrol->value.integer.value[0];
  388. if (tas->hw_enabled)
  389. tas3004_set_drc(tas);
  390. mutex_unlock(&tas->mtx);
  391. return 1;
  392. }
  393. static struct snd_kcontrol_new drc_switch_control = {
  394. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  395. .name = "DRC Range Switch",
  396. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  397. .info = tas_snd_drc_switch_info,
  398. .get = tas_snd_drc_switch_get,
  399. .put = tas_snd_drc_switch_put,
  400. };
  401. static int tas_snd_capture_source_info(struct snd_kcontrol *kcontrol,
  402. struct snd_ctl_elem_info *uinfo)
  403. {
  404. static char *texts[] = { "Line-In", "Microphone" };
  405. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  406. uinfo->count = 1;
  407. uinfo->value.enumerated.items = 2;
  408. if (uinfo->value.enumerated.item > 1)
  409. uinfo->value.enumerated.item = 1;
  410. strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  411. return 0;
  412. }
  413. static int tas_snd_capture_source_get(struct snd_kcontrol *kcontrol,
  414. struct snd_ctl_elem_value *ucontrol)
  415. {
  416. struct tas *tas = snd_kcontrol_chip(kcontrol);
  417. mutex_lock(&tas->mtx);
  418. ucontrol->value.enumerated.item[0] = !!(tas->acr & TAS_ACR_INPUT_B);
  419. mutex_unlock(&tas->mtx);
  420. return 0;
  421. }
  422. static int tas_snd_capture_source_put(struct snd_kcontrol *kcontrol,
  423. struct snd_ctl_elem_value *ucontrol)
  424. {
  425. struct tas *tas = snd_kcontrol_chip(kcontrol);
  426. int oldacr;
  427. mutex_lock(&tas->mtx);
  428. oldacr = tas->acr;
  429. /*
  430. * Despite what the data sheet says in one place, the
  431. * TAS_ACR_B_MONAUREAL bit forces mono output even when
  432. * input A (line in) is selected.
  433. */
  434. tas->acr &= ~(TAS_ACR_INPUT_B | TAS_ACR_B_MONAUREAL);
  435. if (ucontrol->value.enumerated.item[0])
  436. tas->acr |= TAS_ACR_INPUT_B | TAS_ACR_B_MONAUREAL |
  437. TAS_ACR_B_MON_SEL_RIGHT;
  438. if (oldacr == tas->acr) {
  439. mutex_unlock(&tas->mtx);
  440. return 0;
  441. }
  442. if (tas->hw_enabled)
  443. tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
  444. mutex_unlock(&tas->mtx);
  445. return 1;
  446. }
  447. static struct snd_kcontrol_new capture_source_control = {
  448. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  449. /* If we name this 'Input Source', it properly shows up in
  450. * alsamixer as a selection, * but it's shown under the
  451. * 'Playback' category.
  452. * If I name it 'Capture Source', it shows up in strange
  453. * ways (two bools of which one can be selected at a
  454. * time) but at least it's shown in the 'Capture'
  455. * category.
  456. * I was told that this was due to backward compatibility,
  457. * but I don't understand then why the mangling is *not*
  458. * done when I name it "Input Source".....
  459. */
  460. .name = "Capture Source",
  461. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  462. .info = tas_snd_capture_source_info,
  463. .get = tas_snd_capture_source_get,
  464. .put = tas_snd_capture_source_put,
  465. };
  466. static int tas_snd_treble_info(struct snd_kcontrol *kcontrol,
  467. struct snd_ctl_elem_info *uinfo)
  468. {
  469. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  470. uinfo->count = 1;
  471. uinfo->value.integer.min = TAS3004_TREBLE_MIN;
  472. uinfo->value.integer.max = TAS3004_TREBLE_MAX;
  473. return 0;
  474. }
  475. static int tas_snd_treble_get(struct snd_kcontrol *kcontrol,
  476. struct snd_ctl_elem_value *ucontrol)
  477. {
  478. struct tas *tas = snd_kcontrol_chip(kcontrol);
  479. mutex_lock(&tas->mtx);
  480. ucontrol->value.integer.value[0] = tas->treble;
  481. mutex_unlock(&tas->mtx);
  482. return 0;
  483. }
  484. static int tas_snd_treble_put(struct snd_kcontrol *kcontrol,
  485. struct snd_ctl_elem_value *ucontrol)
  486. {
  487. struct tas *tas = snd_kcontrol_chip(kcontrol);
  488. mutex_lock(&tas->mtx);
  489. if (tas->treble == ucontrol->value.integer.value[0]) {
  490. mutex_unlock(&tas->mtx);
  491. return 0;
  492. }
  493. tas->treble = ucontrol->value.integer.value[0];
  494. if (tas->hw_enabled)
  495. tas_set_treble(tas);
  496. mutex_unlock(&tas->mtx);
  497. return 1;
  498. }
  499. static struct snd_kcontrol_new treble_control = {
  500. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  501. .name = "Treble",
  502. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  503. .info = tas_snd_treble_info,
  504. .get = tas_snd_treble_get,
  505. .put = tas_snd_treble_put,
  506. };
  507. static int tas_snd_bass_info(struct snd_kcontrol *kcontrol,
  508. struct snd_ctl_elem_info *uinfo)
  509. {
  510. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  511. uinfo->count = 1;
  512. uinfo->value.integer.min = TAS3004_BASS_MIN;
  513. uinfo->value.integer.max = TAS3004_BASS_MAX;
  514. return 0;
  515. }
  516. static int tas_snd_bass_get(struct snd_kcontrol *kcontrol,
  517. struct snd_ctl_elem_value *ucontrol)
  518. {
  519. struct tas *tas = snd_kcontrol_chip(kcontrol);
  520. mutex_lock(&tas->mtx);
  521. ucontrol->value.integer.value[0] = tas->bass;
  522. mutex_unlock(&tas->mtx);
  523. return 0;
  524. }
  525. static int tas_snd_bass_put(struct snd_kcontrol *kcontrol,
  526. struct snd_ctl_elem_value *ucontrol)
  527. {
  528. struct tas *tas = snd_kcontrol_chip(kcontrol);
  529. mutex_lock(&tas->mtx);
  530. if (tas->bass == ucontrol->value.integer.value[0]) {
  531. mutex_unlock(&tas->mtx);
  532. return 0;
  533. }
  534. tas->bass = ucontrol->value.integer.value[0];
  535. if (tas->hw_enabled)
  536. tas_set_bass(tas);
  537. mutex_unlock(&tas->mtx);
  538. return 1;
  539. }
  540. static struct snd_kcontrol_new bass_control = {
  541. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  542. .name = "Bass",
  543. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  544. .info = tas_snd_bass_info,
  545. .get = tas_snd_bass_get,
  546. .put = tas_snd_bass_put,
  547. };
  548. static struct transfer_info tas_transfers[] = {
  549. {
  550. /* input */
  551. .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_BE |
  552. SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S24_BE,
  553. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  554. .transfer_in = 1,
  555. },
  556. {
  557. /* output */
  558. .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_BE |
  559. SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S24_BE,
  560. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  561. .transfer_in = 0,
  562. },
  563. {}
  564. };
  565. static int tas_usable(struct codec_info_item *cii,
  566. struct transfer_info *ti,
  567. struct transfer_info *out)
  568. {
  569. return 1;
  570. }
  571. static int tas_reset_init(struct tas *tas)
  572. {
  573. u8 tmp;
  574. tas->codec.gpio->methods->all_amps_off(tas->codec.gpio);
  575. msleep(5);
  576. tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
  577. msleep(5);
  578. tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 1);
  579. msleep(20);
  580. tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
  581. msleep(10);
  582. tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio);
  583. tmp = TAS_MCS_SCLK64 | TAS_MCS_SPORT_MODE_I2S | TAS_MCS_SPORT_WL_24BIT;
  584. if (tas_write_reg(tas, TAS_REG_MCS, 1, &tmp))
  585. goto outerr;
  586. tas->acr |= TAS_ACR_ANALOG_PDOWN;
  587. if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr))
  588. goto outerr;
  589. tmp = 0;
  590. if (tas_write_reg(tas, TAS_REG_MCS2, 1, &tmp))
  591. goto outerr;
  592. tas3004_set_drc(tas);
  593. /* Set treble & bass to 0dB */
  594. tas->treble = TAS3004_TREBLE_ZERO;
  595. tas->bass = TAS3004_BASS_ZERO;
  596. tas_set_treble(tas);
  597. tas_set_bass(tas);
  598. tas->acr &= ~TAS_ACR_ANALOG_PDOWN;
  599. if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr))
  600. goto outerr;
  601. return 0;
  602. outerr:
  603. return -ENODEV;
  604. }
  605. static int tas_switch_clock(struct codec_info_item *cii, enum clock_switch clock)
  606. {
  607. struct tas *tas = cii->codec_data;
  608. switch(clock) {
  609. case CLOCK_SWITCH_PREPARE_SLAVE:
  610. /* Clocks are going away, mute mute mute */
  611. tas->codec.gpio->methods->all_amps_off(tas->codec.gpio);
  612. tas->hw_enabled = 0;
  613. break;
  614. case CLOCK_SWITCH_SLAVE:
  615. /* Clocks are back, re-init the codec */
  616. mutex_lock(&tas->mtx);
  617. tas_reset_init(tas);
  618. tas_set_volume(tas);
  619. tas_set_mixer(tas);
  620. tas->hw_enabled = 1;
  621. tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio);
  622. mutex_unlock(&tas->mtx);
  623. break;
  624. default:
  625. /* doesn't happen as of now */
  626. return -EINVAL;
  627. }
  628. return 0;
  629. }
  630. #ifdef CONFIG_PM
  631. /* we are controlled via i2c and assume that is always up
  632. * If that wasn't the case, we'd have to suspend once
  633. * our i2c device is suspended, and then take note of that! */
  634. static int tas_suspend(struct tas *tas)
  635. {
  636. mutex_lock(&tas->mtx);
  637. tas->hw_enabled = 0;
  638. tas->acr |= TAS_ACR_ANALOG_PDOWN;
  639. tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
  640. mutex_unlock(&tas->mtx);
  641. return 0;
  642. }
  643. static int tas_resume(struct tas *tas)
  644. {
  645. /* reset codec */
  646. mutex_lock(&tas->mtx);
  647. tas_reset_init(tas);
  648. tas_set_volume(tas);
  649. tas_set_mixer(tas);
  650. tas->hw_enabled = 1;
  651. mutex_unlock(&tas->mtx);
  652. return 0;
  653. }
  654. static int _tas_suspend(struct codec_info_item *cii, pm_message_t state)
  655. {
  656. return tas_suspend(cii->codec_data);
  657. }
  658. static int _tas_resume(struct codec_info_item *cii)
  659. {
  660. return tas_resume(cii->codec_data);
  661. }
  662. #else /* CONFIG_PM */
  663. #define _tas_suspend NULL
  664. #define _tas_resume NULL
  665. #endif /* CONFIG_PM */
  666. static struct codec_info tas_codec_info = {
  667. .transfers = tas_transfers,
  668. /* in theory, we can drive it at 512 too...
  669. * but so far the framework doesn't allow
  670. * for that and I don't see much point in it. */
  671. .sysclock_factor = 256,
  672. /* same here, could be 32 for just one 16 bit format */
  673. .bus_factor = 64,
  674. .owner = THIS_MODULE,
  675. .usable = tas_usable,
  676. .switch_clock = tas_switch_clock,
  677. .suspend = _tas_suspend,
  678. .resume = _tas_resume,
  679. };
  680. static int tas_init_codec(struct aoa_codec *codec)
  681. {
  682. struct tas *tas = codec_to_tas(codec);
  683. int err;
  684. if (!tas->codec.gpio || !tas->codec.gpio->methods) {
  685. printk(KERN_ERR PFX "gpios not assigned!!\n");
  686. return -EINVAL;
  687. }
  688. mutex_lock(&tas->mtx);
  689. if (tas_reset_init(tas)) {
  690. printk(KERN_ERR PFX "tas failed to initialise\n");
  691. mutex_unlock(&tas->mtx);
  692. return -ENXIO;
  693. }
  694. tas->hw_enabled = 1;
  695. mutex_unlock(&tas->mtx);
  696. if (tas->codec.soundbus_dev->attach_codec(tas->codec.soundbus_dev,
  697. aoa_get_card(),
  698. &tas_codec_info, tas)) {
  699. printk(KERN_ERR PFX "error attaching tas to soundbus\n");
  700. return -ENODEV;
  701. }
  702. if (aoa_snd_device_new(SNDRV_DEV_LOWLEVEL, tas, &ops)) {
  703. printk(KERN_ERR PFX "failed to create tas snd device!\n");
  704. return -ENODEV;
  705. }
  706. err = aoa_snd_ctl_add(snd_ctl_new1(&volume_control, tas));
  707. if (err)
  708. goto error;
  709. err = aoa_snd_ctl_add(snd_ctl_new1(&mute_control, tas));
  710. if (err)
  711. goto error;
  712. err = aoa_snd_ctl_add(snd_ctl_new1(&pcm1_control, tas));
  713. if (err)
  714. goto error;
  715. err = aoa_snd_ctl_add(snd_ctl_new1(&monitor_control, tas));
  716. if (err)
  717. goto error;
  718. err = aoa_snd_ctl_add(snd_ctl_new1(&capture_source_control, tas));
  719. if (err)
  720. goto error;
  721. err = aoa_snd_ctl_add(snd_ctl_new1(&drc_range_control, tas));
  722. if (err)
  723. goto error;
  724. err = aoa_snd_ctl_add(snd_ctl_new1(&drc_switch_control, tas));
  725. if (err)
  726. goto error;
  727. err = aoa_snd_ctl_add(snd_ctl_new1(&treble_control, tas));
  728. if (err)
  729. goto error;
  730. err = aoa_snd_ctl_add(snd_ctl_new1(&bass_control, tas));
  731. if (err)
  732. goto error;
  733. return 0;
  734. error:
  735. tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
  736. snd_device_free(aoa_get_card(), tas);
  737. return err;
  738. }
  739. static void tas_exit_codec(struct aoa_codec *codec)
  740. {
  741. struct tas *tas = codec_to_tas(codec);
  742. if (!tas->codec.soundbus_dev)
  743. return;
  744. tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
  745. }
  746. static struct i2c_driver tas_driver;
  747. static int tas_create(struct i2c_adapter *adapter,
  748. struct device_node *node,
  749. int addr)
  750. {
  751. struct tas *tas;
  752. tas = kzalloc(sizeof(struct tas), GFP_KERNEL);
  753. if (!tas)
  754. return -ENOMEM;
  755. mutex_init(&tas->mtx);
  756. tas->i2c.driver = &tas_driver;
  757. tas->i2c.adapter = adapter;
  758. tas->i2c.addr = addr;
  759. /* seems that half is a saner default */
  760. tas->drc_range = TAS3004_DRC_MAX / 2;
  761. strlcpy(tas->i2c.name, "tas audio codec", I2C_NAME_SIZE);
  762. if (i2c_attach_client(&tas->i2c)) {
  763. printk(KERN_ERR PFX "failed to attach to i2c\n");
  764. goto fail;
  765. }
  766. strlcpy(tas->codec.name, "tas", MAX_CODEC_NAME_LEN);
  767. tas->codec.owner = THIS_MODULE;
  768. tas->codec.init = tas_init_codec;
  769. tas->codec.exit = tas_exit_codec;
  770. tas->codec.node = of_node_get(node);
  771. if (aoa_codec_register(&tas->codec)) {
  772. goto detach;
  773. }
  774. printk(KERN_DEBUG
  775. "snd-aoa-codec-tas: tas found, addr 0x%02x on %s\n",
  776. addr, node->full_name);
  777. return 0;
  778. detach:
  779. i2c_detach_client(&tas->i2c);
  780. fail:
  781. mutex_destroy(&tas->mtx);
  782. kfree(tas);
  783. return -EINVAL;
  784. }
  785. static int tas_i2c_attach(struct i2c_adapter *adapter)
  786. {
  787. struct device_node *busnode, *dev = NULL;
  788. struct pmac_i2c_bus *bus;
  789. bus = pmac_i2c_adapter_to_bus(adapter);
  790. if (bus == NULL)
  791. return -ENODEV;
  792. busnode = pmac_i2c_get_bus_node(bus);
  793. while ((dev = of_get_next_child(busnode, dev)) != NULL) {
  794. if (of_device_is_compatible(dev, "tas3004")) {
  795. const u32 *addr;
  796. printk(KERN_DEBUG PFX "found tas3004\n");
  797. addr = of_get_property(dev, "reg", NULL);
  798. if (!addr)
  799. continue;
  800. return tas_create(adapter, dev, ((*addr) >> 1) & 0x7f);
  801. }
  802. /* older machines have no 'codec' node with a 'compatible'
  803. * property that says 'tas3004', they just have a 'deq'
  804. * node without any such property... */
  805. if (strcmp(dev->name, "deq") == 0) {
  806. const u32 *_addr;
  807. u32 addr;
  808. printk(KERN_DEBUG PFX "found 'deq' node\n");
  809. _addr = of_get_property(dev, "i2c-address", NULL);
  810. if (!_addr)
  811. continue;
  812. addr = ((*_addr) >> 1) & 0x7f;
  813. /* now, if the address doesn't match any of the two
  814. * that a tas3004 can have, we cannot handle this.
  815. * I doubt it ever happens but hey. */
  816. if (addr != 0x34 && addr != 0x35)
  817. continue;
  818. return tas_create(adapter, dev, addr);
  819. }
  820. }
  821. return -ENODEV;
  822. }
  823. static int tas_i2c_detach(struct i2c_client *client)
  824. {
  825. struct tas *tas = container_of(client, struct tas, i2c);
  826. int err;
  827. u8 tmp = TAS_ACR_ANALOG_PDOWN;
  828. if ((err = i2c_detach_client(client)))
  829. return err;
  830. aoa_codec_unregister(&tas->codec);
  831. of_node_put(tas->codec.node);
  832. /* power down codec chip */
  833. tas_write_reg(tas, TAS_REG_ACR, 1, &tmp);
  834. mutex_destroy(&tas->mtx);
  835. kfree(tas);
  836. return 0;
  837. }
  838. static struct i2c_driver tas_driver = {
  839. .driver = {
  840. .name = "aoa_codec_tas",
  841. .owner = THIS_MODULE,
  842. },
  843. .attach_adapter = tas_i2c_attach,
  844. .detach_client = tas_i2c_detach,
  845. };
  846. static int __init tas_init(void)
  847. {
  848. return i2c_add_driver(&tas_driver);
  849. }
  850. static void __exit tas_exit(void)
  851. {
  852. i2c_del_driver(&tas_driver);
  853. }
  854. module_init(tas_init);
  855. module_exit(tas_exit);