clk-palmas.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Clock driver for Palmas device.
  3. *
  4. * Copyright (c) 2013, NVIDIA Corporation.
  5. * Copyright (c) 2013-2014 Texas Instruments, Inc.
  6. *
  7. * Author: Laxman Dewangan <ldewangan@nvidia.com>
  8. * Peter Ujfalusi <peter.ujfalusi@ti.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation version 2.
  13. *
  14. * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
  15. * whether express or implied; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. */
  19. #include <linux/clk.h>
  20. #include <linux/clk-provider.h>
  21. #include <linux/mfd/palmas.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/of_device.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/slab.h>
  27. #define PALMAS_CLOCK_DT_EXT_CONTROL_ENABLE1 1
  28. #define PALMAS_CLOCK_DT_EXT_CONTROL_ENABLE2 2
  29. #define PALMAS_CLOCK_DT_EXT_CONTROL_NSLEEP 3
  30. struct palmas_clk32k_desc {
  31. const char *clk_name;
  32. unsigned int control_reg;
  33. unsigned int enable_mask;
  34. unsigned int sleep_mask;
  35. unsigned int sleep_reqstr_id;
  36. int delay;
  37. };
  38. struct palmas_clock_info {
  39. struct device *dev;
  40. struct clk_hw hw;
  41. struct palmas *palmas;
  42. const struct palmas_clk32k_desc *clk_desc;
  43. int ext_control_pin;
  44. };
  45. static inline struct palmas_clock_info *to_palmas_clks_info(struct clk_hw *hw)
  46. {
  47. return container_of(hw, struct palmas_clock_info, hw);
  48. }
  49. static unsigned long palmas_clks_recalc_rate(struct clk_hw *hw,
  50. unsigned long parent_rate)
  51. {
  52. return 32768;
  53. }
  54. static int palmas_clks_prepare(struct clk_hw *hw)
  55. {
  56. struct palmas_clock_info *cinfo = to_palmas_clks_info(hw);
  57. int ret;
  58. ret = palmas_update_bits(cinfo->palmas, PALMAS_RESOURCE_BASE,
  59. cinfo->clk_desc->control_reg,
  60. cinfo->clk_desc->enable_mask,
  61. cinfo->clk_desc->enable_mask);
  62. if (ret < 0)
  63. dev_err(cinfo->dev, "Reg 0x%02x update failed, %d\n",
  64. cinfo->clk_desc->control_reg, ret);
  65. else if (cinfo->clk_desc->delay)
  66. udelay(cinfo->clk_desc->delay);
  67. return ret;
  68. }
  69. static void palmas_clks_unprepare(struct clk_hw *hw)
  70. {
  71. struct palmas_clock_info *cinfo = to_palmas_clks_info(hw);
  72. int ret;
  73. /*
  74. * Clock can be disabled through external pin if it is externally
  75. * controlled.
  76. */
  77. if (cinfo->ext_control_pin)
  78. return;
  79. ret = palmas_update_bits(cinfo->palmas, PALMAS_RESOURCE_BASE,
  80. cinfo->clk_desc->control_reg,
  81. cinfo->clk_desc->enable_mask, 0);
  82. if (ret < 0)
  83. dev_err(cinfo->dev, "Reg 0x%02x update failed, %d\n",
  84. cinfo->clk_desc->control_reg, ret);
  85. }
  86. static int palmas_clks_is_prepared(struct clk_hw *hw)
  87. {
  88. struct palmas_clock_info *cinfo = to_palmas_clks_info(hw);
  89. int ret;
  90. u32 val;
  91. if (cinfo->ext_control_pin)
  92. return 1;
  93. ret = palmas_read(cinfo->palmas, PALMAS_RESOURCE_BASE,
  94. cinfo->clk_desc->control_reg, &val);
  95. if (ret < 0) {
  96. dev_err(cinfo->dev, "Reg 0x%02x read failed, %d\n",
  97. cinfo->clk_desc->control_reg, ret);
  98. return ret;
  99. }
  100. return !!(val & cinfo->clk_desc->enable_mask);
  101. }
  102. static struct clk_ops palmas_clks_ops = {
  103. .prepare = palmas_clks_prepare,
  104. .unprepare = palmas_clks_unprepare,
  105. .is_prepared = palmas_clks_is_prepared,
  106. .recalc_rate = palmas_clks_recalc_rate,
  107. };
  108. struct palmas_clks_of_match_data {
  109. struct clk_init_data init;
  110. const struct palmas_clk32k_desc desc;
  111. };
  112. static const struct palmas_clks_of_match_data palmas_of_clk32kg = {
  113. .init = {
  114. .name = "clk32kg",
  115. .ops = &palmas_clks_ops,
  116. .flags = CLK_IGNORE_UNUSED,
  117. },
  118. .desc = {
  119. .clk_name = "clk32kg",
  120. .control_reg = PALMAS_CLK32KG_CTRL,
  121. .enable_mask = PALMAS_CLK32KG_CTRL_MODE_ACTIVE,
  122. .sleep_mask = PALMAS_CLK32KG_CTRL_MODE_SLEEP,
  123. .sleep_reqstr_id = PALMAS_EXTERNAL_REQSTR_ID_CLK32KG,
  124. .delay = 200,
  125. },
  126. };
  127. static const struct palmas_clks_of_match_data palmas_of_clk32kgaudio = {
  128. .init = {
  129. .name = "clk32kgaudio",
  130. .ops = &palmas_clks_ops,
  131. .flags = CLK_IGNORE_UNUSED,
  132. },
  133. .desc = {
  134. .clk_name = "clk32kgaudio",
  135. .control_reg = PALMAS_CLK32KGAUDIO_CTRL,
  136. .enable_mask = PALMAS_CLK32KG_CTRL_MODE_ACTIVE,
  137. .sleep_mask = PALMAS_CLK32KG_CTRL_MODE_SLEEP,
  138. .sleep_reqstr_id = PALMAS_EXTERNAL_REQSTR_ID_CLK32KGAUDIO,
  139. .delay = 200,
  140. },
  141. };
  142. static const struct of_device_id palmas_clks_of_match[] = {
  143. {
  144. .compatible = "ti,palmas-clk32kg",
  145. .data = &palmas_of_clk32kg,
  146. },
  147. {
  148. .compatible = "ti,palmas-clk32kgaudio",
  149. .data = &palmas_of_clk32kgaudio,
  150. },
  151. { },
  152. };
  153. MODULE_DEVICE_TABLE(of, palmas_clks_of_match);
  154. static void palmas_clks_get_clk_data(struct platform_device *pdev,
  155. struct palmas_clock_info *cinfo)
  156. {
  157. struct device_node *node = pdev->dev.of_node;
  158. unsigned int prop;
  159. int ret;
  160. ret = of_property_read_u32(node, "ti,external-sleep-control",
  161. &prop);
  162. if (ret)
  163. return;
  164. switch (prop) {
  165. case PALMAS_CLOCK_DT_EXT_CONTROL_ENABLE1:
  166. prop = PALMAS_EXT_CONTROL_ENABLE1;
  167. break;
  168. case PALMAS_CLOCK_DT_EXT_CONTROL_ENABLE2:
  169. prop = PALMAS_EXT_CONTROL_ENABLE2;
  170. break;
  171. case PALMAS_CLOCK_DT_EXT_CONTROL_NSLEEP:
  172. prop = PALMAS_EXT_CONTROL_NSLEEP;
  173. break;
  174. default:
  175. dev_warn(&pdev->dev, "%s: Invalid ext control option: %u\n",
  176. node->name, prop);
  177. prop = 0;
  178. break;
  179. }
  180. cinfo->ext_control_pin = prop;
  181. }
  182. static int palmas_clks_init_configure(struct palmas_clock_info *cinfo)
  183. {
  184. int ret;
  185. ret = palmas_update_bits(cinfo->palmas, PALMAS_RESOURCE_BASE,
  186. cinfo->clk_desc->control_reg,
  187. cinfo->clk_desc->sleep_mask, 0);
  188. if (ret < 0) {
  189. dev_err(cinfo->dev, "Reg 0x%02x update failed, %d\n",
  190. cinfo->clk_desc->control_reg, ret);
  191. return ret;
  192. }
  193. if (cinfo->ext_control_pin) {
  194. ret = clk_prepare(cinfo->hw.clk);
  195. if (ret < 0) {
  196. dev_err(cinfo->dev, "Clock prep failed, %d\n", ret);
  197. return ret;
  198. }
  199. ret = palmas_ext_control_req_config(cinfo->palmas,
  200. cinfo->clk_desc->sleep_reqstr_id,
  201. cinfo->ext_control_pin, true);
  202. if (ret < 0) {
  203. dev_err(cinfo->dev, "Ext config for %s failed, %d\n",
  204. cinfo->clk_desc->clk_name, ret);
  205. return ret;
  206. }
  207. }
  208. return ret;
  209. }
  210. static int palmas_clks_probe(struct platform_device *pdev)
  211. {
  212. struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
  213. struct device_node *node = pdev->dev.of_node;
  214. const struct palmas_clks_of_match_data *match_data;
  215. struct palmas_clock_info *cinfo;
  216. int ret;
  217. match_data = of_device_get_match_data(&pdev->dev);
  218. if (!match_data)
  219. return 1;
  220. cinfo = devm_kzalloc(&pdev->dev, sizeof(*cinfo), GFP_KERNEL);
  221. if (!cinfo)
  222. return -ENOMEM;
  223. palmas_clks_get_clk_data(pdev, cinfo);
  224. platform_set_drvdata(pdev, cinfo);
  225. cinfo->dev = &pdev->dev;
  226. cinfo->palmas = palmas;
  227. cinfo->clk_desc = &match_data->desc;
  228. cinfo->hw.init = &match_data->init;
  229. ret = devm_clk_hw_register(&pdev->dev, &cinfo->hw);
  230. if (ret) {
  231. dev_err(&pdev->dev, "Fail to register clock %s, %d\n",
  232. match_data->desc.clk_name, ret);
  233. return ret;
  234. }
  235. ret = palmas_clks_init_configure(cinfo);
  236. if (ret < 0) {
  237. dev_err(&pdev->dev, "Clock config failed, %d\n", ret);
  238. return ret;
  239. }
  240. ret = of_clk_add_hw_provider(node, of_clk_hw_simple_get, &cinfo->hw);
  241. if (ret < 0)
  242. dev_err(&pdev->dev, "Fail to add clock driver, %d\n", ret);
  243. return ret;
  244. }
  245. static int palmas_clks_remove(struct platform_device *pdev)
  246. {
  247. of_clk_del_provider(pdev->dev.of_node);
  248. return 0;
  249. }
  250. static struct platform_driver palmas_clks_driver = {
  251. .driver = {
  252. .name = "palmas-clk",
  253. .of_match_table = palmas_clks_of_match,
  254. },
  255. .probe = palmas_clks_probe,
  256. .remove = palmas_clks_remove,
  257. };
  258. module_platform_driver(palmas_clks_driver);
  259. MODULE_DESCRIPTION("Clock driver for Palmas Series Devices");
  260. MODULE_ALIAS("platform:palmas-clk");
  261. MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
  262. MODULE_LICENSE("GPL v2");