snd-aoa-codec-tas.c 25 KB

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