tegra30_ahub.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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/slab.h>
  27. #include <linux/clk/tegra.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. dma_addr_t *fiforeg,
  85. unsigned int *reqsel)
  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. *fiforeg = ahub->apbif_addr + TEGRA30_AHUB_CHANNEL_RXFIFO +
  97. (channel * TEGRA30_AHUB_CHANNEL_RXFIFO_STRIDE);
  98. *reqsel = ahub->dma_sel + channel;
  99. reg = TEGRA30_AHUB_CHANNEL_CTRL +
  100. (channel * TEGRA30_AHUB_CHANNEL_CTRL_STRIDE);
  101. val = tegra30_apbif_read(reg);
  102. val &= ~(TEGRA30_AHUB_CHANNEL_CTRL_RX_THRESHOLD_MASK |
  103. TEGRA30_AHUB_CHANNEL_CTRL_RX_PACK_MASK);
  104. val |= (7 << TEGRA30_AHUB_CHANNEL_CTRL_RX_THRESHOLD_SHIFT) |
  105. TEGRA30_AHUB_CHANNEL_CTRL_RX_PACK_EN |
  106. TEGRA30_AHUB_CHANNEL_CTRL_RX_PACK_16;
  107. tegra30_apbif_write(reg, val);
  108. cif_conf.threshold = 0;
  109. cif_conf.audio_channels = 2;
  110. cif_conf.client_channels = 2;
  111. cif_conf.audio_bits = TEGRA30_AUDIOCIF_BITS_16;
  112. cif_conf.client_bits = TEGRA30_AUDIOCIF_BITS_16;
  113. cif_conf.expand = 0;
  114. cif_conf.stereo_conv = 0;
  115. cif_conf.replicate = 0;
  116. cif_conf.direction = TEGRA30_AUDIOCIF_DIRECTION_RX;
  117. cif_conf.truncate = 0;
  118. cif_conf.mono_conv = 0;
  119. reg = TEGRA30_AHUB_CIF_RX_CTRL +
  120. (channel * TEGRA30_AHUB_CIF_RX_CTRL_STRIDE);
  121. ahub->soc_data->set_audio_cif(ahub->regmap_apbif, reg, &cif_conf);
  122. return 0;
  123. }
  124. EXPORT_SYMBOL_GPL(tegra30_ahub_allocate_rx_fifo);
  125. int tegra30_ahub_enable_rx_fifo(enum tegra30_ahub_rxcif rxcif)
  126. {
  127. int channel = rxcif - TEGRA30_AHUB_RXCIF_APBIF_RX0;
  128. int reg, val;
  129. reg = TEGRA30_AHUB_CHANNEL_CTRL +
  130. (channel * TEGRA30_AHUB_CHANNEL_CTRL_STRIDE);
  131. val = tegra30_apbif_read(reg);
  132. val |= TEGRA30_AHUB_CHANNEL_CTRL_RX_EN;
  133. tegra30_apbif_write(reg, val);
  134. return 0;
  135. }
  136. EXPORT_SYMBOL_GPL(tegra30_ahub_enable_rx_fifo);
  137. int tegra30_ahub_disable_rx_fifo(enum tegra30_ahub_rxcif rxcif)
  138. {
  139. int channel = rxcif - TEGRA30_AHUB_RXCIF_APBIF_RX0;
  140. int reg, val;
  141. reg = TEGRA30_AHUB_CHANNEL_CTRL +
  142. (channel * TEGRA30_AHUB_CHANNEL_CTRL_STRIDE);
  143. val = tegra30_apbif_read(reg);
  144. val &= ~TEGRA30_AHUB_CHANNEL_CTRL_RX_EN;
  145. tegra30_apbif_write(reg, val);
  146. return 0;
  147. }
  148. EXPORT_SYMBOL_GPL(tegra30_ahub_disable_rx_fifo);
  149. int tegra30_ahub_free_rx_fifo(enum tegra30_ahub_rxcif rxcif)
  150. {
  151. int channel = rxcif - TEGRA30_AHUB_RXCIF_APBIF_RX0;
  152. __clear_bit(channel, ahub->rx_usage);
  153. return 0;
  154. }
  155. EXPORT_SYMBOL_GPL(tegra30_ahub_free_rx_fifo);
  156. int tegra30_ahub_allocate_tx_fifo(enum tegra30_ahub_txcif *txcif,
  157. dma_addr_t *fiforeg,
  158. unsigned int *reqsel)
  159. {
  160. int channel;
  161. u32 reg, val;
  162. struct tegra30_ahub_cif_conf cif_conf;
  163. channel = find_first_zero_bit(ahub->tx_usage,
  164. TEGRA30_AHUB_CHANNEL_CTRL_COUNT);
  165. if (channel >= TEGRA30_AHUB_CHANNEL_CTRL_COUNT)
  166. return -EBUSY;
  167. __set_bit(channel, ahub->tx_usage);
  168. *txcif = TEGRA30_AHUB_TXCIF_APBIF_TX0 + channel;
  169. *fiforeg = ahub->apbif_addr + TEGRA30_AHUB_CHANNEL_TXFIFO +
  170. (channel * TEGRA30_AHUB_CHANNEL_TXFIFO_STRIDE);
  171. *reqsel = ahub->dma_sel + channel;
  172. reg = TEGRA30_AHUB_CHANNEL_CTRL +
  173. (channel * TEGRA30_AHUB_CHANNEL_CTRL_STRIDE);
  174. val = tegra30_apbif_read(reg);
  175. val &= ~(TEGRA30_AHUB_CHANNEL_CTRL_TX_THRESHOLD_MASK |
  176. TEGRA30_AHUB_CHANNEL_CTRL_TX_PACK_MASK);
  177. val |= (7 << TEGRA30_AHUB_CHANNEL_CTRL_TX_THRESHOLD_SHIFT) |
  178. TEGRA30_AHUB_CHANNEL_CTRL_TX_PACK_EN |
  179. TEGRA30_AHUB_CHANNEL_CTRL_TX_PACK_16;
  180. tegra30_apbif_write(reg, val);
  181. cif_conf.threshold = 0;
  182. cif_conf.audio_channels = 2;
  183. cif_conf.client_channels = 2;
  184. cif_conf.audio_bits = TEGRA30_AUDIOCIF_BITS_16;
  185. cif_conf.client_bits = TEGRA30_AUDIOCIF_BITS_16;
  186. cif_conf.expand = 0;
  187. cif_conf.stereo_conv = 0;
  188. cif_conf.replicate = 0;
  189. cif_conf.direction = TEGRA30_AUDIOCIF_DIRECTION_TX;
  190. cif_conf.truncate = 0;
  191. cif_conf.mono_conv = 0;
  192. reg = TEGRA30_AHUB_CIF_TX_CTRL +
  193. (channel * TEGRA30_AHUB_CIF_TX_CTRL_STRIDE);
  194. ahub->soc_data->set_audio_cif(ahub->regmap_apbif, reg, &cif_conf);
  195. return 0;
  196. }
  197. EXPORT_SYMBOL_GPL(tegra30_ahub_allocate_tx_fifo);
  198. int tegra30_ahub_enable_tx_fifo(enum tegra30_ahub_txcif txcif)
  199. {
  200. int channel = txcif - TEGRA30_AHUB_TXCIF_APBIF_TX0;
  201. int reg, val;
  202. reg = TEGRA30_AHUB_CHANNEL_CTRL +
  203. (channel * TEGRA30_AHUB_CHANNEL_CTRL_STRIDE);
  204. val = tegra30_apbif_read(reg);
  205. val |= TEGRA30_AHUB_CHANNEL_CTRL_TX_EN;
  206. tegra30_apbif_write(reg, val);
  207. return 0;
  208. }
  209. EXPORT_SYMBOL_GPL(tegra30_ahub_enable_tx_fifo);
  210. int tegra30_ahub_disable_tx_fifo(enum tegra30_ahub_txcif txcif)
  211. {
  212. int channel = txcif - TEGRA30_AHUB_TXCIF_APBIF_TX0;
  213. int reg, val;
  214. reg = TEGRA30_AHUB_CHANNEL_CTRL +
  215. (channel * TEGRA30_AHUB_CHANNEL_CTRL_STRIDE);
  216. val = tegra30_apbif_read(reg);
  217. val &= ~TEGRA30_AHUB_CHANNEL_CTRL_TX_EN;
  218. tegra30_apbif_write(reg, val);
  219. return 0;
  220. }
  221. EXPORT_SYMBOL_GPL(tegra30_ahub_disable_tx_fifo);
  222. int tegra30_ahub_free_tx_fifo(enum tegra30_ahub_txcif txcif)
  223. {
  224. int channel = txcif - TEGRA30_AHUB_TXCIF_APBIF_TX0;
  225. __clear_bit(channel, ahub->tx_usage);
  226. return 0;
  227. }
  228. EXPORT_SYMBOL_GPL(tegra30_ahub_free_tx_fifo);
  229. int tegra30_ahub_set_rx_cif_source(enum tegra30_ahub_rxcif rxcif,
  230. enum tegra30_ahub_txcif txcif)
  231. {
  232. int channel = rxcif - TEGRA30_AHUB_RXCIF_APBIF_RX0;
  233. int reg;
  234. reg = TEGRA30_AHUB_AUDIO_RX +
  235. (channel * TEGRA30_AHUB_AUDIO_RX_STRIDE);
  236. tegra30_audio_write(reg, 1 << txcif);
  237. return 0;
  238. }
  239. EXPORT_SYMBOL_GPL(tegra30_ahub_set_rx_cif_source);
  240. int tegra30_ahub_unset_rx_cif_source(enum tegra30_ahub_rxcif rxcif)
  241. {
  242. int channel = rxcif - TEGRA30_AHUB_RXCIF_APBIF_RX0;
  243. int reg;
  244. reg = TEGRA30_AHUB_AUDIO_RX +
  245. (channel * TEGRA30_AHUB_AUDIO_RX_STRIDE);
  246. tegra30_audio_write(reg, 0);
  247. return 0;
  248. }
  249. EXPORT_SYMBOL_GPL(tegra30_ahub_unset_rx_cif_source);
  250. #define CLK_LIST_MASK_TEGRA30 BIT(0)
  251. #define CLK_LIST_MASK_TEGRA114 BIT(1)
  252. #define CLK_LIST_MASK_TEGRA30_OR_LATER \
  253. (CLK_LIST_MASK_TEGRA30 | CLK_LIST_MASK_TEGRA114)
  254. static const struct {
  255. const char *clk_name;
  256. u32 clk_list_mask;
  257. } configlink_clocks[] = {
  258. { "i2s0", CLK_LIST_MASK_TEGRA30_OR_LATER },
  259. { "i2s1", CLK_LIST_MASK_TEGRA30_OR_LATER },
  260. { "i2s2", CLK_LIST_MASK_TEGRA30_OR_LATER },
  261. { "i2s3", CLK_LIST_MASK_TEGRA30_OR_LATER },
  262. { "i2s4", CLK_LIST_MASK_TEGRA30_OR_LATER },
  263. { "dam0", CLK_LIST_MASK_TEGRA30_OR_LATER },
  264. { "dam1", CLK_LIST_MASK_TEGRA30_OR_LATER },
  265. { "dam2", CLK_LIST_MASK_TEGRA30_OR_LATER },
  266. { "spdif_in", CLK_LIST_MASK_TEGRA30_OR_LATER },
  267. { "amx", CLK_LIST_MASK_TEGRA114 },
  268. { "adx", CLK_LIST_MASK_TEGRA114 },
  269. };
  270. #define LAST_REG(name) \
  271. (TEGRA30_AHUB_##name + \
  272. (TEGRA30_AHUB_##name##_STRIDE * TEGRA30_AHUB_##name##_COUNT) - 4)
  273. #define REG_IN_ARRAY(reg, name) \
  274. ((reg >= TEGRA30_AHUB_##name) && \
  275. (reg <= LAST_REG(name) && \
  276. (!((reg - TEGRA30_AHUB_##name) % TEGRA30_AHUB_##name##_STRIDE))))
  277. static bool tegra30_ahub_apbif_wr_rd_reg(struct device *dev, unsigned int reg)
  278. {
  279. switch (reg) {
  280. case TEGRA30_AHUB_CONFIG_LINK_CTRL:
  281. case TEGRA30_AHUB_MISC_CTRL:
  282. case TEGRA30_AHUB_APBDMA_LIVE_STATUS:
  283. case TEGRA30_AHUB_I2S_LIVE_STATUS:
  284. case TEGRA30_AHUB_SPDIF_LIVE_STATUS:
  285. case TEGRA30_AHUB_I2S_INT_MASK:
  286. case TEGRA30_AHUB_DAM_INT_MASK:
  287. case TEGRA30_AHUB_SPDIF_INT_MASK:
  288. case TEGRA30_AHUB_APBIF_INT_MASK:
  289. case TEGRA30_AHUB_I2S_INT_STATUS:
  290. case TEGRA30_AHUB_DAM_INT_STATUS:
  291. case TEGRA30_AHUB_SPDIF_INT_STATUS:
  292. case TEGRA30_AHUB_APBIF_INT_STATUS:
  293. case TEGRA30_AHUB_I2S_INT_SOURCE:
  294. case TEGRA30_AHUB_DAM_INT_SOURCE:
  295. case TEGRA30_AHUB_SPDIF_INT_SOURCE:
  296. case TEGRA30_AHUB_APBIF_INT_SOURCE:
  297. case TEGRA30_AHUB_I2S_INT_SET:
  298. case TEGRA30_AHUB_DAM_INT_SET:
  299. case TEGRA30_AHUB_SPDIF_INT_SET:
  300. case TEGRA30_AHUB_APBIF_INT_SET:
  301. return true;
  302. default:
  303. break;
  304. };
  305. if (REG_IN_ARRAY(reg, CHANNEL_CTRL) ||
  306. REG_IN_ARRAY(reg, CHANNEL_CLEAR) ||
  307. REG_IN_ARRAY(reg, CHANNEL_STATUS) ||
  308. REG_IN_ARRAY(reg, CHANNEL_TXFIFO) ||
  309. REG_IN_ARRAY(reg, CHANNEL_RXFIFO) ||
  310. REG_IN_ARRAY(reg, CIF_TX_CTRL) ||
  311. REG_IN_ARRAY(reg, CIF_RX_CTRL) ||
  312. REG_IN_ARRAY(reg, DAM_LIVE_STATUS))
  313. return true;
  314. return false;
  315. }
  316. static bool tegra30_ahub_apbif_volatile_reg(struct device *dev,
  317. unsigned int reg)
  318. {
  319. switch (reg) {
  320. case TEGRA30_AHUB_CONFIG_LINK_CTRL:
  321. case TEGRA30_AHUB_MISC_CTRL:
  322. case TEGRA30_AHUB_APBDMA_LIVE_STATUS:
  323. case TEGRA30_AHUB_I2S_LIVE_STATUS:
  324. case TEGRA30_AHUB_SPDIF_LIVE_STATUS:
  325. case TEGRA30_AHUB_I2S_INT_STATUS:
  326. case TEGRA30_AHUB_DAM_INT_STATUS:
  327. case TEGRA30_AHUB_SPDIF_INT_STATUS:
  328. case TEGRA30_AHUB_APBIF_INT_STATUS:
  329. case TEGRA30_AHUB_I2S_INT_SET:
  330. case TEGRA30_AHUB_DAM_INT_SET:
  331. case TEGRA30_AHUB_SPDIF_INT_SET:
  332. case TEGRA30_AHUB_APBIF_INT_SET:
  333. return true;
  334. default:
  335. break;
  336. };
  337. if (REG_IN_ARRAY(reg, CHANNEL_CLEAR) ||
  338. REG_IN_ARRAY(reg, CHANNEL_STATUS) ||
  339. REG_IN_ARRAY(reg, CHANNEL_TXFIFO) ||
  340. REG_IN_ARRAY(reg, CHANNEL_RXFIFO) ||
  341. REG_IN_ARRAY(reg, DAM_LIVE_STATUS))
  342. return true;
  343. return false;
  344. }
  345. static bool tegra30_ahub_apbif_precious_reg(struct device *dev,
  346. unsigned int reg)
  347. {
  348. if (REG_IN_ARRAY(reg, CHANNEL_TXFIFO) ||
  349. REG_IN_ARRAY(reg, CHANNEL_RXFIFO))
  350. return true;
  351. return false;
  352. }
  353. static const struct regmap_config tegra30_ahub_apbif_regmap_config = {
  354. .name = "apbif",
  355. .reg_bits = 32,
  356. .val_bits = 32,
  357. .reg_stride = 4,
  358. .max_register = TEGRA30_AHUB_APBIF_INT_SET,
  359. .writeable_reg = tegra30_ahub_apbif_wr_rd_reg,
  360. .readable_reg = tegra30_ahub_apbif_wr_rd_reg,
  361. .volatile_reg = tegra30_ahub_apbif_volatile_reg,
  362. .precious_reg = tegra30_ahub_apbif_precious_reg,
  363. .cache_type = REGCACHE_RBTREE,
  364. };
  365. static bool tegra30_ahub_ahub_wr_rd_reg(struct device *dev, unsigned int reg)
  366. {
  367. if (REG_IN_ARRAY(reg, AUDIO_RX))
  368. return true;
  369. return false;
  370. }
  371. static const struct regmap_config tegra30_ahub_ahub_regmap_config = {
  372. .name = "ahub",
  373. .reg_bits = 32,
  374. .val_bits = 32,
  375. .reg_stride = 4,
  376. .max_register = LAST_REG(AUDIO_RX),
  377. .writeable_reg = tegra30_ahub_ahub_wr_rd_reg,
  378. .readable_reg = tegra30_ahub_ahub_wr_rd_reg,
  379. .cache_type = REGCACHE_RBTREE,
  380. };
  381. static struct tegra30_ahub_soc_data soc_data_tegra30 = {
  382. .clk_list_mask = CLK_LIST_MASK_TEGRA30,
  383. .set_audio_cif = tegra30_ahub_set_cif,
  384. };
  385. static struct tegra30_ahub_soc_data soc_data_tegra114 = {
  386. .clk_list_mask = CLK_LIST_MASK_TEGRA114,
  387. .set_audio_cif = tegra30_ahub_set_cif,
  388. };
  389. static struct tegra30_ahub_soc_data soc_data_tegra124 = {
  390. .clk_list_mask = CLK_LIST_MASK_TEGRA114,
  391. .set_audio_cif = tegra124_ahub_set_cif,
  392. };
  393. static const struct of_device_id tegra30_ahub_of_match[] = {
  394. { .compatible = "nvidia,tegra124-ahub", .data = &soc_data_tegra124 },
  395. { .compatible = "nvidia,tegra114-ahub", .data = &soc_data_tegra114 },
  396. { .compatible = "nvidia,tegra30-ahub", .data = &soc_data_tegra30 },
  397. {},
  398. };
  399. static int tegra30_ahub_probe(struct platform_device *pdev)
  400. {
  401. const struct of_device_id *match;
  402. const struct tegra30_ahub_soc_data *soc_data;
  403. struct clk *clk;
  404. int i;
  405. struct resource *res0, *res1, *region;
  406. u32 of_dma[2];
  407. void __iomem *regs_apbif, *regs_ahub;
  408. int ret = 0;
  409. if (ahub)
  410. return -ENODEV;
  411. match = of_match_device(tegra30_ahub_of_match, &pdev->dev);
  412. if (!match)
  413. return -EINVAL;
  414. soc_data = match->data;
  415. /*
  416. * The AHUB hosts a register bus: the "configlink". For this to
  417. * operate correctly, all devices on this bus must be out of reset.
  418. * Ensure that here.
  419. */
  420. for (i = 0; i < ARRAY_SIZE(configlink_clocks); i++) {
  421. if (!(configlink_clocks[i].clk_list_mask &
  422. soc_data->clk_list_mask))
  423. continue;
  424. clk = clk_get(&pdev->dev, configlink_clocks[i].clk_name);
  425. if (IS_ERR(clk)) {
  426. dev_err(&pdev->dev, "Can't get clock %s\n",
  427. configlink_clocks[i].clk_name);
  428. ret = PTR_ERR(clk);
  429. goto err;
  430. }
  431. tegra_periph_reset_deassert(clk);
  432. clk_put(clk);
  433. }
  434. ahub = devm_kzalloc(&pdev->dev, sizeof(struct tegra30_ahub),
  435. GFP_KERNEL);
  436. if (!ahub) {
  437. dev_err(&pdev->dev, "Can't allocate tegra30_ahub\n");
  438. ret = -ENOMEM;
  439. goto err;
  440. }
  441. dev_set_drvdata(&pdev->dev, ahub);
  442. ahub->soc_data = soc_data;
  443. ahub->dev = &pdev->dev;
  444. ahub->clk_d_audio = clk_get(&pdev->dev, "d_audio");
  445. if (IS_ERR(ahub->clk_d_audio)) {
  446. dev_err(&pdev->dev, "Can't retrieve ahub d_audio clock\n");
  447. ret = PTR_ERR(ahub->clk_d_audio);
  448. goto err;
  449. }
  450. ahub->clk_apbif = clk_get(&pdev->dev, "apbif");
  451. if (IS_ERR(ahub->clk_apbif)) {
  452. dev_err(&pdev->dev, "Can't retrieve ahub apbif clock\n");
  453. ret = PTR_ERR(ahub->clk_apbif);
  454. goto err_clk_put_d_audio;
  455. }
  456. if (of_property_read_u32_array(pdev->dev.of_node,
  457. "nvidia,dma-request-selector",
  458. of_dma, 2) < 0) {
  459. dev_err(&pdev->dev,
  460. "Missing property nvidia,dma-request-selector\n");
  461. ret = -ENODEV;
  462. goto err_clk_put_d_audio;
  463. }
  464. ahub->dma_sel = of_dma[1];
  465. res0 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  466. if (!res0) {
  467. dev_err(&pdev->dev, "No apbif memory resource\n");
  468. ret = -ENODEV;
  469. goto err_clk_put_apbif;
  470. }
  471. region = devm_request_mem_region(&pdev->dev, res0->start,
  472. resource_size(res0), DRV_NAME);
  473. if (!region) {
  474. dev_err(&pdev->dev, "request region apbif failed\n");
  475. ret = -EBUSY;
  476. goto err_clk_put_apbif;
  477. }
  478. ahub->apbif_addr = res0->start;
  479. regs_apbif = devm_ioremap(&pdev->dev, res0->start,
  480. resource_size(res0));
  481. if (!regs_apbif) {
  482. dev_err(&pdev->dev, "ioremap apbif failed\n");
  483. ret = -ENOMEM;
  484. goto err_clk_put_apbif;
  485. }
  486. ahub->regmap_apbif = devm_regmap_init_mmio(&pdev->dev, regs_apbif,
  487. &tegra30_ahub_apbif_regmap_config);
  488. if (IS_ERR(ahub->regmap_apbif)) {
  489. dev_err(&pdev->dev, "apbif regmap init failed\n");
  490. ret = PTR_ERR(ahub->regmap_apbif);
  491. goto err_clk_put_apbif;
  492. }
  493. regcache_cache_only(ahub->regmap_apbif, true);
  494. res1 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  495. if (!res1) {
  496. dev_err(&pdev->dev, "No ahub memory resource\n");
  497. ret = -ENODEV;
  498. goto err_clk_put_apbif;
  499. }
  500. region = devm_request_mem_region(&pdev->dev, res1->start,
  501. resource_size(res1), DRV_NAME);
  502. if (!region) {
  503. dev_err(&pdev->dev, "request region ahub failed\n");
  504. ret = -EBUSY;
  505. goto err_clk_put_apbif;
  506. }
  507. regs_ahub = devm_ioremap(&pdev->dev, res1->start,
  508. resource_size(res1));
  509. if (!regs_ahub) {
  510. dev_err(&pdev->dev, "ioremap ahub failed\n");
  511. ret = -ENOMEM;
  512. goto err_clk_put_apbif;
  513. }
  514. ahub->regmap_ahub = devm_regmap_init_mmio(&pdev->dev, regs_ahub,
  515. &tegra30_ahub_ahub_regmap_config);
  516. if (IS_ERR(ahub->regmap_ahub)) {
  517. dev_err(&pdev->dev, "ahub regmap init failed\n");
  518. ret = PTR_ERR(ahub->regmap_ahub);
  519. goto err_clk_put_apbif;
  520. }
  521. regcache_cache_only(ahub->regmap_ahub, true);
  522. pm_runtime_enable(&pdev->dev);
  523. if (!pm_runtime_enabled(&pdev->dev)) {
  524. ret = tegra30_ahub_runtime_resume(&pdev->dev);
  525. if (ret)
  526. goto err_pm_disable;
  527. }
  528. of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
  529. return 0;
  530. err_pm_disable:
  531. pm_runtime_disable(&pdev->dev);
  532. err_clk_put_apbif:
  533. clk_put(ahub->clk_apbif);
  534. err_clk_put_d_audio:
  535. clk_put(ahub->clk_d_audio);
  536. ahub = NULL;
  537. err:
  538. return ret;
  539. }
  540. static int tegra30_ahub_remove(struct platform_device *pdev)
  541. {
  542. if (!ahub)
  543. return -ENODEV;
  544. pm_runtime_disable(&pdev->dev);
  545. if (!pm_runtime_status_suspended(&pdev->dev))
  546. tegra30_ahub_runtime_suspend(&pdev->dev);
  547. clk_put(ahub->clk_apbif);
  548. clk_put(ahub->clk_d_audio);
  549. ahub = NULL;
  550. return 0;
  551. }
  552. #ifdef CONFIG_PM_SLEEP
  553. static int tegra30_ahub_suspend(struct device *dev)
  554. {
  555. regcache_mark_dirty(ahub->regmap_ahub);
  556. regcache_mark_dirty(ahub->regmap_apbif);
  557. return 0;
  558. }
  559. static int tegra30_ahub_resume(struct device *dev)
  560. {
  561. int ret;
  562. ret = pm_runtime_get_sync(dev);
  563. if (ret < 0)
  564. return ret;
  565. ret = regcache_sync(ahub->regmap_ahub);
  566. ret |= regcache_sync(ahub->regmap_apbif);
  567. pm_runtime_put(dev);
  568. return ret;
  569. }
  570. #endif
  571. static const struct dev_pm_ops tegra30_ahub_pm_ops = {
  572. SET_RUNTIME_PM_OPS(tegra30_ahub_runtime_suspend,
  573. tegra30_ahub_runtime_resume, NULL)
  574. SET_SYSTEM_SLEEP_PM_OPS(tegra30_ahub_suspend, tegra30_ahub_resume)
  575. };
  576. static struct platform_driver tegra30_ahub_driver = {
  577. .probe = tegra30_ahub_probe,
  578. .remove = tegra30_ahub_remove,
  579. .driver = {
  580. .name = DRV_NAME,
  581. .owner = THIS_MODULE,
  582. .of_match_table = tegra30_ahub_of_match,
  583. .pm = &tegra30_ahub_pm_ops,
  584. },
  585. };
  586. module_platform_driver(tegra30_ahub_driver);
  587. void tegra30_ahub_set_cif(struct regmap *regmap, unsigned int reg,
  588. struct tegra30_ahub_cif_conf *conf)
  589. {
  590. unsigned int value;
  591. value = (conf->threshold <<
  592. TEGRA30_AUDIOCIF_CTRL_FIFO_THRESHOLD_SHIFT) |
  593. ((conf->audio_channels - 1) <<
  594. TEGRA30_AUDIOCIF_CTRL_AUDIO_CHANNELS_SHIFT) |
  595. ((conf->client_channels - 1) <<
  596. TEGRA30_AUDIOCIF_CTRL_CLIENT_CHANNELS_SHIFT) |
  597. (conf->audio_bits <<
  598. TEGRA30_AUDIOCIF_CTRL_AUDIO_BITS_SHIFT) |
  599. (conf->client_bits <<
  600. TEGRA30_AUDIOCIF_CTRL_CLIENT_BITS_SHIFT) |
  601. (conf->expand <<
  602. TEGRA30_AUDIOCIF_CTRL_EXPAND_SHIFT) |
  603. (conf->stereo_conv <<
  604. TEGRA30_AUDIOCIF_CTRL_STEREO_CONV_SHIFT) |
  605. (conf->replicate <<
  606. TEGRA30_AUDIOCIF_CTRL_REPLICATE_SHIFT) |
  607. (conf->direction <<
  608. TEGRA30_AUDIOCIF_CTRL_DIRECTION_SHIFT) |
  609. (conf->truncate <<
  610. TEGRA30_AUDIOCIF_CTRL_TRUNCATE_SHIFT) |
  611. (conf->mono_conv <<
  612. TEGRA30_AUDIOCIF_CTRL_MONO_CONV_SHIFT);
  613. regmap_write(regmap, reg, value);
  614. }
  615. EXPORT_SYMBOL_GPL(tegra30_ahub_set_cif);
  616. void tegra124_ahub_set_cif(struct regmap *regmap, unsigned int reg,
  617. struct tegra30_ahub_cif_conf *conf)
  618. {
  619. unsigned int value;
  620. value = (conf->threshold <<
  621. TEGRA124_AUDIOCIF_CTRL_FIFO_THRESHOLD_SHIFT) |
  622. ((conf->audio_channels - 1) <<
  623. TEGRA124_AUDIOCIF_CTRL_AUDIO_CHANNELS_SHIFT) |
  624. ((conf->client_channels - 1) <<
  625. TEGRA124_AUDIOCIF_CTRL_CLIENT_CHANNELS_SHIFT) |
  626. (conf->audio_bits <<
  627. TEGRA30_AUDIOCIF_CTRL_AUDIO_BITS_SHIFT) |
  628. (conf->client_bits <<
  629. TEGRA30_AUDIOCIF_CTRL_CLIENT_BITS_SHIFT) |
  630. (conf->expand <<
  631. TEGRA30_AUDIOCIF_CTRL_EXPAND_SHIFT) |
  632. (conf->stereo_conv <<
  633. TEGRA30_AUDIOCIF_CTRL_STEREO_CONV_SHIFT) |
  634. (conf->replicate <<
  635. TEGRA30_AUDIOCIF_CTRL_REPLICATE_SHIFT) |
  636. (conf->direction <<
  637. TEGRA30_AUDIOCIF_CTRL_DIRECTION_SHIFT) |
  638. (conf->truncate <<
  639. TEGRA30_AUDIOCIF_CTRL_TRUNCATE_SHIFT) |
  640. (conf->mono_conv <<
  641. TEGRA30_AUDIOCIF_CTRL_MONO_CONV_SHIFT);
  642. regmap_write(regmap, reg, value);
  643. }
  644. EXPORT_SYMBOL_GPL(tegra124_ahub_set_cif);
  645. MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
  646. MODULE_DESCRIPTION("Tegra30 AHUB driver");
  647. MODULE_LICENSE("GPL v2");
  648. MODULE_ALIAS("platform:" DRV_NAME);
  649. MODULE_DEVICE_TABLE(of, tegra30_ahub_of_match);