skl-ssp-clk.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright(c) 2015-17 Intel Corporation
  3. /*
  4. * skl-ssp-clk.c - ASoC skylake ssp clock driver
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/err.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/clk-provider.h>
  11. #include <linux/clkdev.h>
  12. #include <sound/intel-nhlt.h>
  13. #include "skl.h"
  14. #include "skl-ssp-clk.h"
  15. #include "skl-topology.h"
  16. #define to_skl_clk(_hw) container_of(_hw, struct skl_clk, hw)
  17. struct skl_clk_parent {
  18. struct clk_hw *hw;
  19. struct clk_lookup *lookup;
  20. };
  21. struct skl_clk {
  22. struct clk_hw hw;
  23. struct clk_lookup *lookup;
  24. unsigned long rate;
  25. struct skl_clk_pdata *pdata;
  26. u32 id;
  27. };
  28. struct skl_clk_data {
  29. struct skl_clk_parent parent[SKL_MAX_CLK_SRC];
  30. struct skl_clk *clk[SKL_MAX_CLK_CNT];
  31. u8 avail_clk_cnt;
  32. };
  33. static int skl_get_clk_type(u32 index)
  34. {
  35. switch (index) {
  36. case 0 ... (SKL_SCLK_OFS - 1):
  37. return SKL_MCLK;
  38. case SKL_SCLK_OFS ... (SKL_SCLKFS_OFS - 1):
  39. return SKL_SCLK;
  40. case SKL_SCLKFS_OFS ... (SKL_MAX_CLK_CNT - 1):
  41. return SKL_SCLK_FS;
  42. default:
  43. return -EINVAL;
  44. }
  45. }
  46. static int skl_get_vbus_id(u32 index, u8 clk_type)
  47. {
  48. switch (clk_type) {
  49. case SKL_MCLK:
  50. return index;
  51. case SKL_SCLK:
  52. return index - SKL_SCLK_OFS;
  53. case SKL_SCLK_FS:
  54. return index - SKL_SCLKFS_OFS;
  55. default:
  56. return -EINVAL;
  57. }
  58. }
  59. static void skl_fill_clk_ipc(struct skl_clk_rate_cfg_table *rcfg, u8 clk_type)
  60. {
  61. struct nhlt_fmt_cfg *fmt_cfg;
  62. union skl_clk_ctrl_ipc *ipc;
  63. struct wav_fmt *wfmt;
  64. if (!rcfg)
  65. return;
  66. ipc = &rcfg->dma_ctl_ipc;
  67. if (clk_type == SKL_SCLK_FS) {
  68. fmt_cfg = (struct nhlt_fmt_cfg *)rcfg->config;
  69. wfmt = &fmt_cfg->fmt_ext.fmt;
  70. /* Remove TLV Header size */
  71. ipc->sclk_fs.hdr.size = sizeof(struct skl_dmactrl_sclkfs_cfg) -
  72. sizeof(struct skl_tlv_hdr);
  73. ipc->sclk_fs.sampling_frequency = wfmt->samples_per_sec;
  74. ipc->sclk_fs.bit_depth = wfmt->bits_per_sample;
  75. ipc->sclk_fs.valid_bit_depth =
  76. fmt_cfg->fmt_ext.sample.valid_bits_per_sample;
  77. ipc->sclk_fs.number_of_channels = wfmt->channels;
  78. } else {
  79. ipc->mclk.hdr.type = DMA_CLK_CONTROLS;
  80. /* Remove TLV Header size */
  81. ipc->mclk.hdr.size = sizeof(struct skl_dmactrl_mclk_cfg) -
  82. sizeof(struct skl_tlv_hdr);
  83. }
  84. }
  85. /* Sends dma control IPC to turn the clock ON/OFF */
  86. static int skl_send_clk_dma_control(struct skl_dev *skl,
  87. struct skl_clk_rate_cfg_table *rcfg,
  88. u32 vbus_id, u8 clk_type,
  89. bool enable)
  90. {
  91. struct nhlt_specific_cfg *sp_cfg;
  92. u32 i2s_config_size, node_id = 0;
  93. struct nhlt_fmt_cfg *fmt_cfg;
  94. union skl_clk_ctrl_ipc *ipc;
  95. void *i2s_config = NULL;
  96. u8 *data, size;
  97. int ret;
  98. if (!rcfg)
  99. return -EIO;
  100. ipc = &rcfg->dma_ctl_ipc;
  101. fmt_cfg = (struct nhlt_fmt_cfg *)rcfg->config;
  102. sp_cfg = &fmt_cfg->config;
  103. if (clk_type == SKL_SCLK_FS) {
  104. ipc->sclk_fs.hdr.type =
  105. enable ? DMA_TRANSMITION_START : DMA_TRANSMITION_STOP;
  106. data = (u8 *)&ipc->sclk_fs;
  107. size = sizeof(struct skl_dmactrl_sclkfs_cfg);
  108. } else {
  109. /* 1 to enable mclk, 0 to enable sclk */
  110. if (clk_type == SKL_SCLK)
  111. ipc->mclk.mclk = 0;
  112. else
  113. ipc->mclk.mclk = 1;
  114. ipc->mclk.keep_running = enable;
  115. ipc->mclk.warm_up_over = enable;
  116. ipc->mclk.clk_stop_over = !enable;
  117. data = (u8 *)&ipc->mclk;
  118. size = sizeof(struct skl_dmactrl_mclk_cfg);
  119. }
  120. i2s_config_size = sp_cfg->size + size;
  121. i2s_config = kzalloc(i2s_config_size, GFP_KERNEL);
  122. if (!i2s_config)
  123. return -ENOMEM;
  124. /* copy blob */
  125. memcpy(i2s_config, sp_cfg->caps, sp_cfg->size);
  126. /* copy additional dma controls information */
  127. memcpy(i2s_config + sp_cfg->size, data, size);
  128. node_id = ((SKL_DMA_I2S_LINK_INPUT_CLASS << 8) | (vbus_id << 4));
  129. ret = skl_dsp_set_dma_control(skl, (u32 *)i2s_config,
  130. i2s_config_size, node_id);
  131. kfree(i2s_config);
  132. return ret;
  133. }
  134. static struct skl_clk_rate_cfg_table *skl_get_rate_cfg(
  135. struct skl_clk_rate_cfg_table *rcfg,
  136. unsigned long rate)
  137. {
  138. int i;
  139. for (i = 0; (i < SKL_MAX_CLK_RATES) && rcfg[i].rate; i++) {
  140. if (rcfg[i].rate == rate)
  141. return &rcfg[i];
  142. }
  143. return NULL;
  144. }
  145. static int skl_clk_change_status(struct skl_clk *clkdev,
  146. bool enable)
  147. {
  148. struct skl_clk_rate_cfg_table *rcfg;
  149. int vbus_id, clk_type;
  150. clk_type = skl_get_clk_type(clkdev->id);
  151. if (clk_type < 0)
  152. return clk_type;
  153. vbus_id = skl_get_vbus_id(clkdev->id, clk_type);
  154. if (vbus_id < 0)
  155. return vbus_id;
  156. rcfg = skl_get_rate_cfg(clkdev->pdata->ssp_clks[clkdev->id].rate_cfg,
  157. clkdev->rate);
  158. if (!rcfg)
  159. return -EINVAL;
  160. return skl_send_clk_dma_control(clkdev->pdata->pvt_data, rcfg,
  161. vbus_id, clk_type, enable);
  162. }
  163. static int skl_clk_prepare(struct clk_hw *hw)
  164. {
  165. struct skl_clk *clkdev = to_skl_clk(hw);
  166. return skl_clk_change_status(clkdev, true);
  167. }
  168. static void skl_clk_unprepare(struct clk_hw *hw)
  169. {
  170. struct skl_clk *clkdev = to_skl_clk(hw);
  171. skl_clk_change_status(clkdev, false);
  172. }
  173. static int skl_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  174. unsigned long parent_rate)
  175. {
  176. struct skl_clk *clkdev = to_skl_clk(hw);
  177. struct skl_clk_rate_cfg_table *rcfg;
  178. int clk_type;
  179. if (!rate)
  180. return -EINVAL;
  181. rcfg = skl_get_rate_cfg(clkdev->pdata->ssp_clks[clkdev->id].rate_cfg,
  182. rate);
  183. if (!rcfg)
  184. return -EINVAL;
  185. clk_type = skl_get_clk_type(clkdev->id);
  186. if (clk_type < 0)
  187. return clk_type;
  188. skl_fill_clk_ipc(rcfg, clk_type);
  189. clkdev->rate = rate;
  190. return 0;
  191. }
  192. static unsigned long skl_clk_recalc_rate(struct clk_hw *hw,
  193. unsigned long parent_rate)
  194. {
  195. struct skl_clk *clkdev = to_skl_clk(hw);
  196. if (clkdev->rate)
  197. return clkdev->rate;
  198. return 0;
  199. }
  200. /* Not supported by clk driver. Implemented to satisfy clk fw */
  201. static long skl_clk_round_rate(struct clk_hw *hw, unsigned long rate,
  202. unsigned long *parent_rate)
  203. {
  204. return rate;
  205. }
  206. /*
  207. * prepare/unprepare are used instead of enable/disable as IPC will be sent
  208. * in non-atomic context.
  209. */
  210. static const struct clk_ops skl_clk_ops = {
  211. .prepare = skl_clk_prepare,
  212. .unprepare = skl_clk_unprepare,
  213. .set_rate = skl_clk_set_rate,
  214. .round_rate = skl_clk_round_rate,
  215. .recalc_rate = skl_clk_recalc_rate,
  216. };
  217. static void unregister_parent_src_clk(struct skl_clk_parent *pclk,
  218. unsigned int id)
  219. {
  220. while (id--) {
  221. clkdev_drop(pclk[id].lookup);
  222. clk_hw_unregister_fixed_rate(pclk[id].hw);
  223. }
  224. }
  225. static void unregister_src_clk(struct skl_clk_data *dclk)
  226. {
  227. while (dclk->avail_clk_cnt--)
  228. clkdev_drop(dclk->clk[dclk->avail_clk_cnt]->lookup);
  229. }
  230. static int skl_register_parent_clks(struct device *dev,
  231. struct skl_clk_parent *parent,
  232. struct skl_clk_parent_src *pclk)
  233. {
  234. int i, ret;
  235. for (i = 0; i < SKL_MAX_CLK_SRC; i++) {
  236. /* Register Parent clock */
  237. parent[i].hw = clk_hw_register_fixed_rate(dev, pclk[i].name,
  238. pclk[i].parent_name, 0, pclk[i].rate);
  239. if (IS_ERR(parent[i].hw)) {
  240. ret = PTR_ERR(parent[i].hw);
  241. goto err;
  242. }
  243. parent[i].lookup = clkdev_hw_create(parent[i].hw, pclk[i].name,
  244. NULL);
  245. if (!parent[i].lookup) {
  246. clk_hw_unregister_fixed_rate(parent[i].hw);
  247. ret = -ENOMEM;
  248. goto err;
  249. }
  250. }
  251. return 0;
  252. err:
  253. unregister_parent_src_clk(parent, i);
  254. return ret;
  255. }
  256. /* Assign fmt_config to clk_data */
  257. static struct skl_clk *register_skl_clk(struct device *dev,
  258. struct skl_ssp_clk *clk,
  259. struct skl_clk_pdata *clk_pdata, int id)
  260. {
  261. struct clk_init_data init;
  262. struct skl_clk *clkdev;
  263. int ret;
  264. clkdev = devm_kzalloc(dev, sizeof(*clkdev), GFP_KERNEL);
  265. if (!clkdev)
  266. return ERR_PTR(-ENOMEM);
  267. init.name = clk->name;
  268. init.ops = &skl_clk_ops;
  269. init.flags = CLK_SET_RATE_GATE;
  270. init.parent_names = &clk->parent_name;
  271. init.num_parents = 1;
  272. clkdev->hw.init = &init;
  273. clkdev->pdata = clk_pdata;
  274. clkdev->id = id;
  275. ret = devm_clk_hw_register(dev, &clkdev->hw);
  276. if (ret) {
  277. clkdev = ERR_PTR(ret);
  278. return clkdev;
  279. }
  280. clkdev->lookup = clkdev_hw_create(&clkdev->hw, init.name, NULL);
  281. if (!clkdev->lookup)
  282. clkdev = ERR_PTR(-ENOMEM);
  283. return clkdev;
  284. }
  285. static int skl_clk_dev_probe(struct platform_device *pdev)
  286. {
  287. struct device *dev = &pdev->dev;
  288. struct device *parent_dev = dev->parent;
  289. struct skl_clk_parent_src *parent_clks;
  290. struct skl_clk_pdata *clk_pdata;
  291. struct skl_clk_data *data;
  292. struct skl_ssp_clk *clks;
  293. int ret, i;
  294. clk_pdata = dev_get_platdata(&pdev->dev);
  295. parent_clks = clk_pdata->parent_clks;
  296. clks = clk_pdata->ssp_clks;
  297. if (!parent_clks || !clks)
  298. return -EIO;
  299. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  300. if (!data)
  301. return -ENOMEM;
  302. /* Register Parent clock */
  303. ret = skl_register_parent_clks(parent_dev, data->parent, parent_clks);
  304. if (ret < 0)
  305. return ret;
  306. for (i = 0; i < clk_pdata->num_clks; i++) {
  307. /*
  308. * Only register valid clocks
  309. * i.e. for which nhlt entry is present.
  310. */
  311. if (clks[i].rate_cfg[0].rate == 0)
  312. continue;
  313. data->clk[data->avail_clk_cnt] = register_skl_clk(dev,
  314. &clks[i], clk_pdata, i);
  315. if (IS_ERR(data->clk[data->avail_clk_cnt])) {
  316. ret = PTR_ERR(data->clk[data->avail_clk_cnt]);
  317. goto err_unreg_skl_clk;
  318. }
  319. data->avail_clk_cnt++;
  320. }
  321. platform_set_drvdata(pdev, data);
  322. return 0;
  323. err_unreg_skl_clk:
  324. unregister_src_clk(data);
  325. unregister_parent_src_clk(data->parent, SKL_MAX_CLK_SRC);
  326. return ret;
  327. }
  328. static int skl_clk_dev_remove(struct platform_device *pdev)
  329. {
  330. struct skl_clk_data *data;
  331. data = platform_get_drvdata(pdev);
  332. unregister_src_clk(data);
  333. unregister_parent_src_clk(data->parent, SKL_MAX_CLK_SRC);
  334. return 0;
  335. }
  336. static struct platform_driver skl_clk_driver = {
  337. .driver = {
  338. .name = "skl-ssp-clk",
  339. },
  340. .probe = skl_clk_dev_probe,
  341. .remove = skl_clk_dev_remove,
  342. };
  343. module_platform_driver(skl_clk_driver);
  344. MODULE_DESCRIPTION("Skylake clock driver");
  345. MODULE_AUTHOR("Jaikrishna Nemallapudi <[email protected]>");
  346. MODULE_AUTHOR("Subhransu S. Prusty <[email protected]>");
  347. MODULE_LICENSE("GPL v2");
  348. MODULE_ALIAS("platform:skl-ssp-clk");