tegra30_ahub.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. /*
  2. * tegra30_ahub.c - Tegra30 AHUB driver
  3. *
  4. * Copyright (c) 2011,2012, NVIDIA CORPORATION. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/clk.h>
  19. #include <linux/device.h>
  20. #include <linux/io.h>
  21. #include <linux/module.h>
  22. #include <linux/of_platform.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/pm_runtime.h>
  25. #include <linux/regmap.h>
  26. #include <linux/reset.h>
  27. #include <linux/slab.h>
  28. #include <sound/soc.h>
  29. #include "tegra30_ahub.h"
  30. #define DRV_NAME "tegra30-ahub"
  31. static struct tegra30_ahub *ahub;
  32. static inline void tegra30_apbif_write(u32 reg, u32 val)
  33. {
  34. regmap_write(ahub->regmap_apbif, reg, val);
  35. }
  36. static inline u32 tegra30_apbif_read(u32 reg)
  37. {
  38. u32 val;
  39. regmap_read(ahub->regmap_apbif, reg, &val);
  40. return val;
  41. }
  42. static inline void tegra30_audio_write(u32 reg, u32 val)
  43. {
  44. regmap_write(ahub->regmap_ahub, reg, val);
  45. }
  46. static int tegra30_ahub_runtime_suspend(struct device *dev)
  47. {
  48. regcache_cache_only(ahub->regmap_apbif, true);
  49. regcache_cache_only(ahub->regmap_ahub, true);
  50. clk_disable_unprepare(ahub->clk_apbif);
  51. clk_disable_unprepare(ahub->clk_d_audio);
  52. return 0;
  53. }
  54. /*
  55. * clk_apbif isn't required for an I2S<->I2S configuration where no PCM data
  56. * is read from or sent to memory. However, that's not something the rest of
  57. * the driver supports right now, so we'll just treat the two clocks as one
  58. * for now.
  59. *
  60. * These functions should not be a plain ref-count. Instead, each active stream
  61. * contributes some requirement to the minimum clock rate, so starting or
  62. * stopping streams should dynamically adjust the clock as required. However,
  63. * this is not yet implemented.
  64. */
  65. static int tegra30_ahub_runtime_resume(struct device *dev)
  66. {
  67. int ret;
  68. ret = clk_prepare_enable(ahub->clk_d_audio);
  69. if (ret) {
  70. dev_err(dev, "clk_enable d_audio failed: %d\n", ret);
  71. return ret;
  72. }
  73. ret = clk_prepare_enable(ahub->clk_apbif);
  74. if (ret) {
  75. dev_err(dev, "clk_enable apbif failed: %d\n", ret);
  76. clk_disable(ahub->clk_d_audio);
  77. return ret;
  78. }
  79. regcache_cache_only(ahub->regmap_apbif, false);
  80. regcache_cache_only(ahub->regmap_ahub, false);
  81. return 0;
  82. }
  83. int tegra30_ahub_allocate_rx_fifo(enum tegra30_ahub_rxcif *rxcif,
  84. char *dmachan, int dmachan_len,
  85. dma_addr_t *fiforeg)
  86. {
  87. int channel;
  88. u32 reg, val;
  89. struct tegra30_ahub_cif_conf cif_conf;
  90. channel = find_first_zero_bit(ahub->rx_usage,
  91. TEGRA30_AHUB_CHANNEL_CTRL_COUNT);
  92. if (channel >= TEGRA30_AHUB_CHANNEL_CTRL_COUNT)
  93. return -EBUSY;
  94. __set_bit(channel, ahub->rx_usage);
  95. *rxcif = TEGRA30_AHUB_RXCIF_APBIF_RX0 + channel;
  96. snprintf(dmachan, dmachan_len, "rx%d", channel);
  97. *fiforeg = ahub->apbif_addr + TEGRA30_AHUB_CHANNEL_RXFIFO +
  98. (channel * TEGRA30_AHUB_CHANNEL_RXFIFO_STRIDE);
  99. pm_runtime_get_sync(ahub->dev);
  100. reg = TEGRA30_AHUB_CHANNEL_CTRL +
  101. (channel * TEGRA30_AHUB_CHANNEL_CTRL_STRIDE);
  102. val = tegra30_apbif_read(reg);
  103. val &= ~(TEGRA30_AHUB_CHANNEL_CTRL_RX_THRESHOLD_MASK |
  104. TEGRA30_AHUB_CHANNEL_CTRL_RX_PACK_MASK);
  105. val |= (7 << TEGRA30_AHUB_CHANNEL_CTRL_RX_THRESHOLD_SHIFT) |
  106. TEGRA30_AHUB_CHANNEL_CTRL_RX_PACK_EN |
  107. TEGRA30_AHUB_CHANNEL_CTRL_RX_PACK_16;
  108. tegra30_apbif_write(reg, val);
  109. cif_conf.threshold = 0;
  110. cif_conf.audio_channels = 2;
  111. cif_conf.client_channels = 2;
  112. cif_conf.audio_bits = TEGRA30_AUDIOCIF_BITS_16;
  113. cif_conf.client_bits = TEGRA30_AUDIOCIF_BITS_16;
  114. cif_conf.expand = 0;
  115. cif_conf.stereo_conv = 0;
  116. cif_conf.replicate = 0;
  117. cif_conf.direction = TEGRA30_AUDIOCIF_DIRECTION_RX;
  118. cif_conf.truncate = 0;
  119. cif_conf.mono_conv = 0;
  120. reg = TEGRA30_AHUB_CIF_RX_CTRL +
  121. (channel * TEGRA30_AHUB_CIF_RX_CTRL_STRIDE);
  122. ahub->soc_data->set_audio_cif(ahub->regmap_apbif, reg, &cif_conf);
  123. pm_runtime_put(ahub->dev);
  124. return 0;
  125. }
  126. EXPORT_SYMBOL_GPL(tegra30_ahub_allocate_rx_fifo);
  127. int tegra30_ahub_enable_rx_fifo(enum tegra30_ahub_rxcif rxcif)
  128. {
  129. int channel = rxcif - TEGRA30_AHUB_RXCIF_APBIF_RX0;
  130. int reg, val;
  131. pm_runtime_get_sync(ahub->dev);
  132. reg = TEGRA30_AHUB_CHANNEL_CTRL +
  133. (channel * TEGRA30_AHUB_CHANNEL_CTRL_STRIDE);
  134. val = tegra30_apbif_read(reg);
  135. val |= TEGRA30_AHUB_CHANNEL_CTRL_RX_EN;
  136. tegra30_apbif_write(reg, val);
  137. pm_runtime_put(ahub->dev);
  138. return 0;
  139. }
  140. EXPORT_SYMBOL_GPL(tegra30_ahub_enable_rx_fifo);
  141. int tegra30_ahub_disable_rx_fifo(enum tegra30_ahub_rxcif rxcif)
  142. {
  143. int channel = rxcif - TEGRA30_AHUB_RXCIF_APBIF_RX0;
  144. int reg, val;
  145. pm_runtime_get_sync(ahub->dev);
  146. reg = TEGRA30_AHUB_CHANNEL_CTRL +
  147. (channel * TEGRA30_AHUB_CHANNEL_CTRL_STRIDE);
  148. val = tegra30_apbif_read(reg);
  149. val &= ~TEGRA30_AHUB_CHANNEL_CTRL_RX_EN;
  150. tegra30_apbif_write(reg, val);
  151. pm_runtime_put(ahub->dev);
  152. return 0;
  153. }
  154. EXPORT_SYMBOL_GPL(tegra30_ahub_disable_rx_fifo);
  155. int tegra30_ahub_free_rx_fifo(enum tegra30_ahub_rxcif rxcif)
  156. {
  157. int channel = rxcif - TEGRA30_AHUB_RXCIF_APBIF_RX0;
  158. __clear_bit(channel, ahub->rx_usage);
  159. return 0;
  160. }
  161. EXPORT_SYMBOL_GPL(tegra30_ahub_free_rx_fifo);
  162. int tegra30_ahub_allocate_tx_fifo(enum tegra30_ahub_txcif *txcif,
  163. char *dmachan, int dmachan_len,
  164. dma_addr_t *fiforeg)
  165. {
  166. int channel;
  167. u32 reg, val;
  168. struct tegra30_ahub_cif_conf cif_conf;
  169. channel = find_first_zero_bit(ahub->tx_usage,
  170. TEGRA30_AHUB_CHANNEL_CTRL_COUNT);
  171. if (channel >= TEGRA30_AHUB_CHANNEL_CTRL_COUNT)
  172. return -EBUSY;
  173. __set_bit(channel, ahub->tx_usage);
  174. *txcif = TEGRA30_AHUB_TXCIF_APBIF_TX0 + channel;
  175. snprintf(dmachan, dmachan_len, "tx%d", channel);
  176. *fiforeg = ahub->apbif_addr + TEGRA30_AHUB_CHANNEL_TXFIFO +
  177. (channel * TEGRA30_AHUB_CHANNEL_TXFIFO_STRIDE);
  178. pm_runtime_get_sync(ahub->dev);
  179. reg = TEGRA30_AHUB_CHANNEL_CTRL +
  180. (channel * TEGRA30_AHUB_CHANNEL_CTRL_STRIDE);
  181. val = tegra30_apbif_read(reg);
  182. val &= ~(TEGRA30_AHUB_CHANNEL_CTRL_TX_THRESHOLD_MASK |
  183. TEGRA30_AHUB_CHANNEL_CTRL_TX_PACK_MASK);
  184. val |= (7 << TEGRA30_AHUB_CHANNEL_CTRL_TX_THRESHOLD_SHIFT) |
  185. TEGRA30_AHUB_CHANNEL_CTRL_TX_PACK_EN |
  186. TEGRA30_AHUB_CHANNEL_CTRL_TX_PACK_16;
  187. tegra30_apbif_write(reg, val);
  188. cif_conf.threshold = 0;
  189. cif_conf.audio_channels = 2;
  190. cif_conf.client_channels = 2;
  191. cif_conf.audio_bits = TEGRA30_AUDIOCIF_BITS_16;
  192. cif_conf.client_bits = TEGRA30_AUDIOCIF_BITS_16;
  193. cif_conf.expand = 0;
  194. cif_conf.stereo_conv = 0;
  195. cif_conf.replicate = 0;
  196. cif_conf.direction = TEGRA30_AUDIOCIF_DIRECTION_TX;
  197. cif_conf.truncate = 0;
  198. cif_conf.mono_conv = 0;
  199. reg = TEGRA30_AHUB_CIF_TX_CTRL +
  200. (channel * TEGRA30_AHUB_CIF_TX_CTRL_STRIDE);
  201. ahub->soc_data->set_audio_cif(ahub->regmap_apbif, reg, &cif_conf);
  202. pm_runtime_put(ahub->dev);
  203. return 0;
  204. }
  205. EXPORT_SYMBOL_GPL(tegra30_ahub_allocate_tx_fifo);
  206. int tegra30_ahub_enable_tx_fifo(enum tegra30_ahub_txcif txcif)
  207. {
  208. int channel = txcif - TEGRA30_AHUB_TXCIF_APBIF_TX0;
  209. int reg, val;
  210. pm_runtime_get_sync(ahub->dev);
  211. reg = TEGRA30_AHUB_CHANNEL_CTRL +
  212. (channel * TEGRA30_AHUB_CHANNEL_CTRL_STRIDE);
  213. val = tegra30_apbif_read(reg);
  214. val |= TEGRA30_AHUB_CHANNEL_CTRL_TX_EN;
  215. tegra30_apbif_write(reg, val);
  216. pm_runtime_put(ahub->dev);
  217. return 0;
  218. }
  219. EXPORT_SYMBOL_GPL(tegra30_ahub_enable_tx_fifo);
  220. int tegra30_ahub_disable_tx_fifo(enum tegra30_ahub_txcif txcif)
  221. {
  222. int channel = txcif - TEGRA30_AHUB_TXCIF_APBIF_TX0;
  223. int reg, val;
  224. pm_runtime_get_sync(ahub->dev);
  225. reg = TEGRA30_AHUB_CHANNEL_CTRL +
  226. (channel * TEGRA30_AHUB_CHANNEL_CTRL_STRIDE);
  227. val = tegra30_apbif_read(reg);
  228. val &= ~TEGRA30_AHUB_CHANNEL_CTRL_TX_EN;
  229. tegra30_apbif_write(reg, val);
  230. pm_runtime_put(ahub->dev);
  231. return 0;
  232. }
  233. EXPORT_SYMBOL_GPL(tegra30_ahub_disable_tx_fifo);
  234. int tegra30_ahub_free_tx_fifo(enum tegra30_ahub_txcif txcif)
  235. {
  236. int channel = txcif - TEGRA30_AHUB_TXCIF_APBIF_TX0;
  237. __clear_bit(channel, ahub->tx_usage);
  238. return 0;
  239. }
  240. EXPORT_SYMBOL_GPL(tegra30_ahub_free_tx_fifo);
  241. int tegra30_ahub_set_rx_cif_source(enum tegra30_ahub_rxcif rxcif,
  242. enum tegra30_ahub_txcif txcif)
  243. {
  244. int channel = rxcif - TEGRA30_AHUB_RXCIF_APBIF_RX0;
  245. int reg;
  246. pm_runtime_get_sync(ahub->dev);
  247. reg = TEGRA30_AHUB_AUDIO_RX +
  248. (channel * TEGRA30_AHUB_AUDIO_RX_STRIDE);
  249. tegra30_audio_write(reg, 1 << txcif);
  250. pm_runtime_put(ahub->dev);
  251. return 0;
  252. }
  253. EXPORT_SYMBOL_GPL(tegra30_ahub_set_rx_cif_source);
  254. int tegra30_ahub_unset_rx_cif_source(enum tegra30_ahub_rxcif rxcif)
  255. {
  256. int channel = rxcif - TEGRA30_AHUB_RXCIF_APBIF_RX0;
  257. int reg;
  258. pm_runtime_get_sync(ahub->dev);
  259. reg = TEGRA30_AHUB_AUDIO_RX +
  260. (channel * TEGRA30_AHUB_AUDIO_RX_STRIDE);
  261. tegra30_audio_write(reg, 0);
  262. pm_runtime_put(ahub->dev);
  263. return 0;
  264. }
  265. EXPORT_SYMBOL_GPL(tegra30_ahub_unset_rx_cif_source);
  266. #define MOD_LIST_MASK_TEGRA30 BIT(0)
  267. #define MOD_LIST_MASK_TEGRA114 BIT(1)
  268. #define MOD_LIST_MASK_TEGRA124 BIT(2)
  269. #define MOD_LIST_MASK_TEGRA30_OR_LATER \
  270. (MOD_LIST_MASK_TEGRA30 | MOD_LIST_MASK_TEGRA114 | \
  271. MOD_LIST_MASK_TEGRA124)
  272. #define MOD_LIST_MASK_TEGRA114_OR_LATER \
  273. (MOD_LIST_MASK_TEGRA114 | MOD_LIST_MASK_TEGRA124)
  274. static const struct {
  275. const char *rst_name;
  276. u32 mod_list_mask;
  277. } configlink_mods[] = {
  278. { "i2s0", MOD_LIST_MASK_TEGRA30_OR_LATER },
  279. { "i2s1", MOD_LIST_MASK_TEGRA30_OR_LATER },
  280. { "i2s2", MOD_LIST_MASK_TEGRA30_OR_LATER },
  281. { "i2s3", MOD_LIST_MASK_TEGRA30_OR_LATER },
  282. { "i2s4", MOD_LIST_MASK_TEGRA30_OR_LATER },
  283. { "dam0", MOD_LIST_MASK_TEGRA30_OR_LATER },
  284. { "dam1", MOD_LIST_MASK_TEGRA30_OR_LATER },
  285. { "dam2", MOD_LIST_MASK_TEGRA30_OR_LATER },
  286. { "spdif", MOD_LIST_MASK_TEGRA30_OR_LATER },
  287. { "amx", MOD_LIST_MASK_TEGRA114_OR_LATER },
  288. { "adx", MOD_LIST_MASK_TEGRA114_OR_LATER },
  289. { "amx1", MOD_LIST_MASK_TEGRA124 },
  290. { "adx1", MOD_LIST_MASK_TEGRA124 },
  291. { "afc0", MOD_LIST_MASK_TEGRA124 },
  292. { "afc1", MOD_LIST_MASK_TEGRA124 },
  293. { "afc2", MOD_LIST_MASK_TEGRA124 },
  294. { "afc3", MOD_LIST_MASK_TEGRA124 },
  295. { "afc4", MOD_LIST_MASK_TEGRA124 },
  296. { "afc5", MOD_LIST_MASK_TEGRA124 },
  297. };
  298. #define LAST_REG(name) \
  299. (TEGRA30_AHUB_##name + \
  300. (TEGRA30_AHUB_##name##_STRIDE * TEGRA30_AHUB_##name##_COUNT) - 4)
  301. #define REG_IN_ARRAY(reg, name) \
  302. ((reg >= TEGRA30_AHUB_##name) && \
  303. (reg <= LAST_REG(name) && \
  304. (!((reg - TEGRA30_AHUB_##name) % TEGRA30_AHUB_##name##_STRIDE))))
  305. static bool tegra30_ahub_apbif_wr_rd_reg(struct device *dev, unsigned int reg)
  306. {
  307. switch (reg) {
  308. case TEGRA30_AHUB_CONFIG_LINK_CTRL:
  309. case TEGRA30_AHUB_MISC_CTRL:
  310. case TEGRA30_AHUB_APBDMA_LIVE_STATUS:
  311. case TEGRA30_AHUB_I2S_LIVE_STATUS:
  312. case TEGRA30_AHUB_SPDIF_LIVE_STATUS:
  313. case TEGRA30_AHUB_I2S_INT_MASK:
  314. case TEGRA30_AHUB_DAM_INT_MASK:
  315. case TEGRA30_AHUB_SPDIF_INT_MASK:
  316. case TEGRA30_AHUB_APBIF_INT_MASK:
  317. case TEGRA30_AHUB_I2S_INT_STATUS:
  318. case TEGRA30_AHUB_DAM_INT_STATUS:
  319. case TEGRA30_AHUB_SPDIF_INT_STATUS:
  320. case TEGRA30_AHUB_APBIF_INT_STATUS:
  321. case TEGRA30_AHUB_I2S_INT_SOURCE:
  322. case TEGRA30_AHUB_DAM_INT_SOURCE:
  323. case TEGRA30_AHUB_SPDIF_INT_SOURCE:
  324. case TEGRA30_AHUB_APBIF_INT_SOURCE:
  325. case TEGRA30_AHUB_I2S_INT_SET:
  326. case TEGRA30_AHUB_DAM_INT_SET:
  327. case TEGRA30_AHUB_SPDIF_INT_SET:
  328. case TEGRA30_AHUB_APBIF_INT_SET:
  329. return true;
  330. default:
  331. break;
  332. }
  333. if (REG_IN_ARRAY(reg, CHANNEL_CTRL) ||
  334. REG_IN_ARRAY(reg, CHANNEL_CLEAR) ||
  335. REG_IN_ARRAY(reg, CHANNEL_STATUS) ||
  336. REG_IN_ARRAY(reg, CHANNEL_TXFIFO) ||
  337. REG_IN_ARRAY(reg, CHANNEL_RXFIFO) ||
  338. REG_IN_ARRAY(reg, CIF_TX_CTRL) ||
  339. REG_IN_ARRAY(reg, CIF_RX_CTRL) ||
  340. REG_IN_ARRAY(reg, DAM_LIVE_STATUS))
  341. return true;
  342. return false;
  343. }
  344. static bool tegra30_ahub_apbif_volatile_reg(struct device *dev,
  345. unsigned int reg)
  346. {
  347. switch (reg) {
  348. case TEGRA30_AHUB_CONFIG_LINK_CTRL:
  349. case TEGRA30_AHUB_MISC_CTRL:
  350. case TEGRA30_AHUB_APBDMA_LIVE_STATUS:
  351. case TEGRA30_AHUB_I2S_LIVE_STATUS:
  352. case TEGRA30_AHUB_SPDIF_LIVE_STATUS:
  353. case TEGRA30_AHUB_I2S_INT_STATUS:
  354. case TEGRA30_AHUB_DAM_INT_STATUS:
  355. case TEGRA30_AHUB_SPDIF_INT_STATUS:
  356. case TEGRA30_AHUB_APBIF_INT_STATUS:
  357. case TEGRA30_AHUB_I2S_INT_SET:
  358. case TEGRA30_AHUB_DAM_INT_SET:
  359. case TEGRA30_AHUB_SPDIF_INT_SET:
  360. case TEGRA30_AHUB_APBIF_INT_SET:
  361. return true;
  362. default:
  363. break;
  364. }
  365. if (REG_IN_ARRAY(reg, CHANNEL_CLEAR) ||
  366. REG_IN_ARRAY(reg, CHANNEL_STATUS) ||
  367. REG_IN_ARRAY(reg, CHANNEL_TXFIFO) ||
  368. REG_IN_ARRAY(reg, CHANNEL_RXFIFO) ||
  369. REG_IN_ARRAY(reg, DAM_LIVE_STATUS))
  370. return true;
  371. return false;
  372. }
  373. static bool tegra30_ahub_apbif_precious_reg(struct device *dev,
  374. unsigned int reg)
  375. {
  376. if (REG_IN_ARRAY(reg, CHANNEL_TXFIFO) ||
  377. REG_IN_ARRAY(reg, CHANNEL_RXFIFO))
  378. return true;
  379. return false;
  380. }
  381. static const struct regmap_config tegra30_ahub_apbif_regmap_config = {
  382. .name = "apbif",
  383. .reg_bits = 32,
  384. .val_bits = 32,
  385. .reg_stride = 4,
  386. .max_register = TEGRA30_AHUB_APBIF_INT_SET,
  387. .writeable_reg = tegra30_ahub_apbif_wr_rd_reg,
  388. .readable_reg = tegra30_ahub_apbif_wr_rd_reg,
  389. .volatile_reg = tegra30_ahub_apbif_volatile_reg,
  390. .precious_reg = tegra30_ahub_apbif_precious_reg,
  391. .cache_type = REGCACHE_FLAT,
  392. };
  393. static bool tegra30_ahub_ahub_wr_rd_reg(struct device *dev, unsigned int reg)
  394. {
  395. if (REG_IN_ARRAY(reg, AUDIO_RX))
  396. return true;
  397. return false;
  398. }
  399. static const struct regmap_config tegra30_ahub_ahub_regmap_config = {
  400. .name = "ahub",
  401. .reg_bits = 32,
  402. .val_bits = 32,
  403. .reg_stride = 4,
  404. .max_register = LAST_REG(AUDIO_RX),
  405. .writeable_reg = tegra30_ahub_ahub_wr_rd_reg,
  406. .readable_reg = tegra30_ahub_ahub_wr_rd_reg,
  407. .cache_type = REGCACHE_FLAT,
  408. };
  409. static struct tegra30_ahub_soc_data soc_data_tegra30 = {
  410. .mod_list_mask = MOD_LIST_MASK_TEGRA30,
  411. .set_audio_cif = tegra30_ahub_set_cif,
  412. };
  413. static struct tegra30_ahub_soc_data soc_data_tegra114 = {
  414. .mod_list_mask = MOD_LIST_MASK_TEGRA114,
  415. .set_audio_cif = tegra30_ahub_set_cif,
  416. };
  417. static struct tegra30_ahub_soc_data soc_data_tegra124 = {
  418. .mod_list_mask = MOD_LIST_MASK_TEGRA124,
  419. .set_audio_cif = tegra124_ahub_set_cif,
  420. };
  421. static const struct of_device_id tegra30_ahub_of_match[] = {
  422. { .compatible = "nvidia,tegra124-ahub", .data = &soc_data_tegra124 },
  423. { .compatible = "nvidia,tegra114-ahub", .data = &soc_data_tegra114 },
  424. { .compatible = "nvidia,tegra30-ahub", .data = &soc_data_tegra30 },
  425. {},
  426. };
  427. static int tegra30_ahub_probe(struct platform_device *pdev)
  428. {
  429. const struct of_device_id *match;
  430. const struct tegra30_ahub_soc_data *soc_data;
  431. struct reset_control *rst;
  432. int i;
  433. struct resource *res0, *res1;
  434. void __iomem *regs_apbif, *regs_ahub;
  435. int ret = 0;
  436. if (ahub)
  437. return -ENODEV;
  438. match = of_match_device(tegra30_ahub_of_match, &pdev->dev);
  439. if (!match)
  440. return -EINVAL;
  441. soc_data = match->data;
  442. /*
  443. * The AHUB hosts a register bus: the "configlink". For this to
  444. * operate correctly, all devices on this bus must be out of reset.
  445. * Ensure that here.
  446. */
  447. for (i = 0; i < ARRAY_SIZE(configlink_mods); i++) {
  448. if (!(configlink_mods[i].mod_list_mask &
  449. soc_data->mod_list_mask))
  450. continue;
  451. rst = reset_control_get(&pdev->dev,
  452. configlink_mods[i].rst_name);
  453. if (IS_ERR(rst)) {
  454. dev_err(&pdev->dev, "Can't get reset %s\n",
  455. configlink_mods[i].rst_name);
  456. ret = PTR_ERR(rst);
  457. return ret;
  458. }
  459. ret = reset_control_deassert(rst);
  460. reset_control_put(rst);
  461. if (ret)
  462. return ret;
  463. }
  464. ahub = devm_kzalloc(&pdev->dev, sizeof(struct tegra30_ahub),
  465. GFP_KERNEL);
  466. if (!ahub) {
  467. dev_err(&pdev->dev, "Can't allocate tegra30_ahub\n");
  468. return -ENOMEM;
  469. }
  470. dev_set_drvdata(&pdev->dev, ahub);
  471. ahub->soc_data = soc_data;
  472. ahub->dev = &pdev->dev;
  473. ahub->clk_d_audio = devm_clk_get(&pdev->dev, "d_audio");
  474. if (IS_ERR(ahub->clk_d_audio)) {
  475. dev_err(&pdev->dev, "Can't retrieve ahub d_audio clock\n");
  476. ret = PTR_ERR(ahub->clk_d_audio);
  477. return ret;
  478. }
  479. ahub->clk_apbif = devm_clk_get(&pdev->dev, "apbif");
  480. if (IS_ERR(ahub->clk_apbif)) {
  481. dev_err(&pdev->dev, "Can't retrieve ahub apbif clock\n");
  482. ret = PTR_ERR(ahub->clk_apbif);
  483. return ret;
  484. }
  485. res0 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  486. regs_apbif = devm_ioremap_resource(&pdev->dev, res0);
  487. if (IS_ERR(regs_apbif))
  488. return PTR_ERR(regs_apbif);
  489. ahub->apbif_addr = res0->start;
  490. ahub->regmap_apbif = devm_regmap_init_mmio(&pdev->dev, regs_apbif,
  491. &tegra30_ahub_apbif_regmap_config);
  492. if (IS_ERR(ahub->regmap_apbif)) {
  493. dev_err(&pdev->dev, "apbif regmap init failed\n");
  494. ret = PTR_ERR(ahub->regmap_apbif);
  495. return ret;
  496. }
  497. regcache_cache_only(ahub->regmap_apbif, true);
  498. res1 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  499. regs_ahub = devm_ioremap_resource(&pdev->dev, res1);
  500. if (IS_ERR(regs_ahub))
  501. return PTR_ERR(regs_ahub);
  502. ahub->regmap_ahub = devm_regmap_init_mmio(&pdev->dev, regs_ahub,
  503. &tegra30_ahub_ahub_regmap_config);
  504. if (IS_ERR(ahub->regmap_ahub)) {
  505. dev_err(&pdev->dev, "ahub regmap init failed\n");
  506. ret = PTR_ERR(ahub->regmap_ahub);
  507. return ret;
  508. }
  509. regcache_cache_only(ahub->regmap_ahub, true);
  510. pm_runtime_enable(&pdev->dev);
  511. if (!pm_runtime_enabled(&pdev->dev)) {
  512. ret = tegra30_ahub_runtime_resume(&pdev->dev);
  513. if (ret)
  514. goto err_pm_disable;
  515. }
  516. of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
  517. return 0;
  518. err_pm_disable:
  519. pm_runtime_disable(&pdev->dev);
  520. return ret;
  521. }
  522. static int tegra30_ahub_remove(struct platform_device *pdev)
  523. {
  524. if (!ahub)
  525. return -ENODEV;
  526. pm_runtime_disable(&pdev->dev);
  527. if (!pm_runtime_status_suspended(&pdev->dev))
  528. tegra30_ahub_runtime_suspend(&pdev->dev);
  529. return 0;
  530. }
  531. #ifdef CONFIG_PM_SLEEP
  532. static int tegra30_ahub_suspend(struct device *dev)
  533. {
  534. regcache_mark_dirty(ahub->regmap_ahub);
  535. regcache_mark_dirty(ahub->regmap_apbif);
  536. return 0;
  537. }
  538. static int tegra30_ahub_resume(struct device *dev)
  539. {
  540. int ret;
  541. ret = pm_runtime_get_sync(dev);
  542. if (ret < 0)
  543. return ret;
  544. ret = regcache_sync(ahub->regmap_ahub);
  545. ret |= regcache_sync(ahub->regmap_apbif);
  546. pm_runtime_put(dev);
  547. return ret;
  548. }
  549. #endif
  550. static const struct dev_pm_ops tegra30_ahub_pm_ops = {
  551. SET_RUNTIME_PM_OPS(tegra30_ahub_runtime_suspend,
  552. tegra30_ahub_runtime_resume, NULL)
  553. SET_SYSTEM_SLEEP_PM_OPS(tegra30_ahub_suspend, tegra30_ahub_resume)
  554. };
  555. static struct platform_driver tegra30_ahub_driver = {
  556. .probe = tegra30_ahub_probe,
  557. .remove = tegra30_ahub_remove,
  558. .driver = {
  559. .name = DRV_NAME,
  560. .of_match_table = tegra30_ahub_of_match,
  561. .pm = &tegra30_ahub_pm_ops,
  562. },
  563. };
  564. module_platform_driver(tegra30_ahub_driver);
  565. void tegra30_ahub_set_cif(struct regmap *regmap, unsigned int reg,
  566. struct tegra30_ahub_cif_conf *conf)
  567. {
  568. unsigned int value;
  569. value = (conf->threshold <<
  570. TEGRA30_AUDIOCIF_CTRL_FIFO_THRESHOLD_SHIFT) |
  571. ((conf->audio_channels - 1) <<
  572. TEGRA30_AUDIOCIF_CTRL_AUDIO_CHANNELS_SHIFT) |
  573. ((conf->client_channels - 1) <<
  574. TEGRA30_AUDIOCIF_CTRL_CLIENT_CHANNELS_SHIFT) |
  575. (conf->audio_bits <<
  576. TEGRA30_AUDIOCIF_CTRL_AUDIO_BITS_SHIFT) |
  577. (conf->client_bits <<
  578. TEGRA30_AUDIOCIF_CTRL_CLIENT_BITS_SHIFT) |
  579. (conf->expand <<
  580. TEGRA30_AUDIOCIF_CTRL_EXPAND_SHIFT) |
  581. (conf->stereo_conv <<
  582. TEGRA30_AUDIOCIF_CTRL_STEREO_CONV_SHIFT) |
  583. (conf->replicate <<
  584. TEGRA30_AUDIOCIF_CTRL_REPLICATE_SHIFT) |
  585. (conf->direction <<
  586. TEGRA30_AUDIOCIF_CTRL_DIRECTION_SHIFT) |
  587. (conf->truncate <<
  588. TEGRA30_AUDIOCIF_CTRL_TRUNCATE_SHIFT) |
  589. (conf->mono_conv <<
  590. TEGRA30_AUDIOCIF_CTRL_MONO_CONV_SHIFT);
  591. regmap_write(regmap, reg, value);
  592. }
  593. EXPORT_SYMBOL_GPL(tegra30_ahub_set_cif);
  594. void tegra124_ahub_set_cif(struct regmap *regmap, unsigned int reg,
  595. struct tegra30_ahub_cif_conf *conf)
  596. {
  597. unsigned int value;
  598. value = (conf->threshold <<
  599. TEGRA124_AUDIOCIF_CTRL_FIFO_THRESHOLD_SHIFT) |
  600. ((conf->audio_channels - 1) <<
  601. TEGRA124_AUDIOCIF_CTRL_AUDIO_CHANNELS_SHIFT) |
  602. ((conf->client_channels - 1) <<
  603. TEGRA124_AUDIOCIF_CTRL_CLIENT_CHANNELS_SHIFT) |
  604. (conf->audio_bits <<
  605. TEGRA30_AUDIOCIF_CTRL_AUDIO_BITS_SHIFT) |
  606. (conf->client_bits <<
  607. TEGRA30_AUDIOCIF_CTRL_CLIENT_BITS_SHIFT) |
  608. (conf->expand <<
  609. TEGRA30_AUDIOCIF_CTRL_EXPAND_SHIFT) |
  610. (conf->stereo_conv <<
  611. TEGRA30_AUDIOCIF_CTRL_STEREO_CONV_SHIFT) |
  612. (conf->replicate <<
  613. TEGRA30_AUDIOCIF_CTRL_REPLICATE_SHIFT) |
  614. (conf->direction <<
  615. TEGRA30_AUDIOCIF_CTRL_DIRECTION_SHIFT) |
  616. (conf->truncate <<
  617. TEGRA30_AUDIOCIF_CTRL_TRUNCATE_SHIFT) |
  618. (conf->mono_conv <<
  619. TEGRA30_AUDIOCIF_CTRL_MONO_CONV_SHIFT);
  620. regmap_write(regmap, reg, value);
  621. }
  622. EXPORT_SYMBOL_GPL(tegra124_ahub_set_cif);
  623. MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
  624. MODULE_DESCRIPTION("Tegra30 AHUB driver");
  625. MODULE_LICENSE("GPL v2");
  626. MODULE_ALIAS("platform:" DRV_NAME);
  627. MODULE_DEVICE_TABLE(of, tegra30_ahub_of_match);