skl-ssp-clk.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. // SPDX-License-Identifier: GPL-2.0
  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 "skl.h"
  13. #include "skl-ssp-clk.h"
  14. #include "skl-topology.h"
  15. #define to_skl_clk(_hw) container_of(_hw, struct skl_clk, hw)
  16. struct skl_clk_parent {
  17. struct clk_hw *hw;
  18. struct clk_lookup *lookup;
  19. };
  20. struct skl_clk {
  21. struct clk_hw hw;
  22. struct clk_lookup *lookup;
  23. unsigned long rate;
  24. struct skl_clk_pdata *pdata;
  25. u32 id;
  26. };
  27. struct skl_clk_data {
  28. struct skl_clk_parent parent[SKL_MAX_CLK_SRC];
  29. struct skl_clk *clk[SKL_MAX_CLK_CNT];
  30. u8 avail_clk_cnt;
  31. };
  32. static int skl_get_clk_type(u32 index)
  33. {
  34. switch (index) {
  35. case 0 ... (SKL_SCLK_OFS - 1):
  36. return SKL_MCLK;
  37. case SKL_SCLK_OFS ... (SKL_SCLKFS_OFS - 1):
  38. return SKL_SCLK;
  39. case SKL_SCLKFS_OFS ... (SKL_MAX_CLK_CNT - 1):
  40. return SKL_SCLK_FS;
  41. default:
  42. return -EINVAL;
  43. }
  44. }
  45. static int skl_get_vbus_id(u32 index, u8 clk_type)
  46. {
  47. switch (clk_type) {
  48. case SKL_MCLK:
  49. return index;
  50. case SKL_SCLK:
  51. return index - SKL_SCLK_OFS;
  52. case SKL_SCLK_FS:
  53. return index - SKL_SCLKFS_OFS;
  54. default:
  55. return -EINVAL;
  56. }
  57. }
  58. static void skl_fill_clk_ipc(struct skl_clk_rate_cfg_table *rcfg, u8 clk_type)
  59. {
  60. struct nhlt_fmt_cfg *fmt_cfg;
  61. union skl_clk_ctrl_ipc *ipc;
  62. struct wav_fmt *wfmt;
  63. if (!rcfg)
  64. return;
  65. ipc = &rcfg->dma_ctl_ipc;
  66. if (clk_type == SKL_SCLK_FS) {
  67. fmt_cfg = (struct nhlt_fmt_cfg *)rcfg->config;
  68. wfmt = &fmt_cfg->fmt_ext.fmt;
  69. /* Remove TLV Header size */
  70. ipc->sclk_fs.hdr.size = sizeof(struct skl_dmactrl_sclkfs_cfg) -
  71. sizeof(struct skl_tlv_hdr);
  72. ipc->sclk_fs.sampling_frequency = wfmt->samples_per_sec;
  73. ipc->sclk_fs.bit_depth = wfmt->bits_per_sample;
  74. ipc->sclk_fs.valid_bit_depth =
  75. fmt_cfg->fmt_ext.sample.valid_bits_per_sample;
  76. ipc->sclk_fs.number_of_channels = wfmt->channels;
  77. } else {
  78. ipc->mclk.hdr.type = DMA_CLK_CONTROLS;
  79. /* Remove TLV Header size */
  80. ipc->mclk.hdr.size = sizeof(struct skl_dmactrl_mclk_cfg) -
  81. sizeof(struct skl_tlv_hdr);
  82. }
  83. }
  84. /* Sends dma control IPC to turn the clock ON/OFF */
  85. static int skl_send_clk_dma_control(struct skl *skl,
  86. struct skl_clk_rate_cfg_table *rcfg,
  87. u32 vbus_id, u8 clk_type,
  88. bool enable)
  89. {
  90. struct nhlt_specific_cfg *sp_cfg;
  91. u32 i2s_config_size, node_id = 0;
  92. struct nhlt_fmt_cfg *fmt_cfg;
  93. union skl_clk_ctrl_ipc *ipc;
  94. void *i2s_config = NULL;
  95. u8 *data, size;
  96. int ret;
  97. if (!rcfg)
  98. return -EIO;
  99. ipc = &rcfg->dma_ctl_ipc;
  100. fmt_cfg = (struct nhlt_fmt_cfg *)rcfg->config;
  101. sp_cfg = &fmt_cfg->config;
  102. if (clk_type == SKL_SCLK_FS) {
  103. ipc->sclk_fs.hdr.type =
  104. enable ? DMA_TRANSMITION_START : DMA_TRANSMITION_STOP;
  105. data = (u8 *)&ipc->sclk_fs;
  106. size = sizeof(struct skl_dmactrl_sclkfs_cfg);
  107. } else {
  108. /* 1 to enable mclk, 0 to enable sclk */
  109. if (clk_type == SKL_SCLK)
  110. ipc->mclk.mclk = 0;
  111. else
  112. ipc->mclk.mclk = 1;
  113. ipc->mclk.keep_running = enable;
  114. ipc->mclk.warm_up_over = enable;
  115. ipc->mclk.clk_stop_over = !enable;
  116. data = (u8 *)&ipc->mclk;
  117. size = sizeof(struct skl_dmactrl_mclk_cfg);
  118. }
  119. i2s_config_size = sp_cfg->size + size;
  120. i2s_config = kzalloc(i2s_config_size, GFP_KERNEL);
  121. if (!i2s_config)
  122. return -ENOMEM;
  123. /* copy blob */
  124. memcpy(i2s_config, sp_cfg->caps, sp_cfg->size);
  125. /* copy additional dma controls information */
  126. memcpy(i2s_config + sp_cfg->size, data, size);
  127. node_id = ((SKL_DMA_I2S_LINK_INPUT_CLASS << 8) | (vbus_id << 4));
  128. ret = skl_dsp_set_dma_control(skl->skl_sst, (u32 *)i2s_config,
  129. i2s_config_size, node_id);
  130. kfree(i2s_config);
  131. return ret;
  132. }
  133. static struct skl_clk_rate_cfg_table *skl_get_rate_cfg(
  134. struct skl_clk_rate_cfg_table *rcfg,
  135. unsigned long rate)
  136. {
  137. int i;
  138. for (i = 0; (i < SKL_MAX_CLK_RATES) && rcfg[i].rate; i++) {
  139. if (rcfg[i].rate == rate)
  140. return &rcfg[i];
  141. }
  142. return NULL;
  143. }
  144. static int skl_clk_change_status(struct skl_clk *clkdev,
  145. bool enable)
  146. {
  147. struct skl_clk_rate_cfg_table *rcfg;
  148. int vbus_id, clk_type;
  149. clk_type = skl_get_clk_type(clkdev->id);
  150. if (clk_type < 0)
  151. return clk_type;
  152. vbus_id = skl_get_vbus_id(clkdev->id, clk_type);
  153. if (vbus_id < 0)
  154. return vbus_id;
  155. rcfg = skl_get_rate_cfg(clkdev->pdata->ssp_clks[clkdev->id].rate_cfg,
  156. clkdev->rate);
  157. if (!rcfg)
  158. return -EINVAL;
  159. return skl_send_clk_dma_control(clkdev->pdata->pvt_data, rcfg,
  160. vbus_id, clk_type, enable);
  161. }
  162. static int skl_clk_prepare(struct clk_hw *hw)
  163. {
  164. struct skl_clk *clkdev = to_skl_clk(hw);
  165. return skl_clk_change_status(clkdev, true);
  166. }
  167. static void skl_clk_unprepare(struct clk_hw *hw)
  168. {
  169. struct skl_clk *clkdev = to_skl_clk(hw);
  170. skl_clk_change_status(clkdev, false);
  171. }
  172. static int skl_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  173. unsigned long parent_rate)
  174. {
  175. struct skl_clk *clkdev = to_skl_clk(hw);
  176. struct skl_clk_rate_cfg_table *rcfg;
  177. int clk_type;
  178. if (!rate)
  179. return -EINVAL;
  180. rcfg = skl_get_rate_cfg(clkdev->pdata->ssp_clks[clkdev->id].rate_cfg,
  181. rate);
  182. if (!rcfg)
  183. return -EINVAL;
  184. clk_type = skl_get_clk_type(clkdev->id);
  185. if (clk_type < 0)
  186. return clk_type;
  187. skl_fill_clk_ipc(rcfg, clk_type);
  188. clkdev->rate = rate;
  189. return 0;
  190. }
  191. static unsigned long skl_clk_recalc_rate(struct clk_hw *hw,
  192. unsigned long parent_rate)
  193. {
  194. struct skl_clk *clkdev = to_skl_clk(hw);
  195. if (clkdev->rate)
  196. return clkdev->rate;
  197. return 0;
  198. }
  199. /* Not supported by clk driver. Implemented to satisfy clk fw */
  200. static long skl_clk_round_rate(struct clk_hw *hw, unsigned long rate,
  201. unsigned long *parent_rate)
  202. {
  203. return rate;
  204. }
  205. /*
  206. * prepare/unprepare are used instead of enable/disable as IPC will be sent
  207. * in non-atomic context.
  208. */
  209. static const struct clk_ops skl_clk_ops = {
  210. .prepare = skl_clk_prepare,
  211. .unprepare = skl_clk_unprepare,
  212. .set_rate = skl_clk_set_rate,
  213. .round_rate = skl_clk_round_rate,
  214. .recalc_rate = skl_clk_recalc_rate,
  215. };
  216. static void unregister_parent_src_clk(struct skl_clk_parent *pclk,
  217. unsigned int id)
  218. {
  219. while (id--) {
  220. clkdev_drop(pclk[id].lookup);
  221. clk_hw_unregister_fixed_rate(pclk[id].hw);
  222. }
  223. }
  224. static void unregister_src_clk(struct skl_clk_data *dclk)
  225. {
  226. u8 cnt = dclk->avail_clk_cnt;
  227. while (cnt--)
  228. clkdev_drop(dclk->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[i] = register_skl_clk(dev, &clks[i], clk_pdata, i);
  314. if (IS_ERR(data->clk[i])) {
  315. ret = PTR_ERR(data->clk[i]);
  316. goto err_unreg_skl_clk;
  317. }
  318. data->avail_clk_cnt++;
  319. }
  320. platform_set_drvdata(pdev, data);
  321. return 0;
  322. err_unreg_skl_clk:
  323. unregister_src_clk(data);
  324. unregister_parent_src_clk(data->parent, SKL_MAX_CLK_SRC);
  325. return ret;
  326. }
  327. static int skl_clk_dev_remove(struct platform_device *pdev)
  328. {
  329. struct skl_clk_data *data;
  330. data = platform_get_drvdata(pdev);
  331. unregister_src_clk(data);
  332. unregister_parent_src_clk(data->parent, SKL_MAX_CLK_SRC);
  333. return 0;
  334. }
  335. static struct platform_driver skl_clk_driver = {
  336. .driver = {
  337. .name = "skl-ssp-clk",
  338. },
  339. .probe = skl_clk_dev_probe,
  340. .remove = skl_clk_dev_remove,
  341. };
  342. module_platform_driver(skl_clk_driver);
  343. MODULE_DESCRIPTION("Skylake clock driver");
  344. MODULE_AUTHOR("Jaikrishna Nemallapudi <jaikrishnax.nemallapudi@intel.com>");
  345. MODULE_AUTHOR("Subhransu S. Prusty <subhransu.s.prusty@intel.com>");
  346. MODULE_LICENSE("GPL v2");
  347. MODULE_ALIAS("platform:skl-ssp-clk");