clk-max77686.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * clk-max77686.c - Clock driver for Maxim 77686/MAX77802
  3. *
  4. * Copyright (C) 2012 Samsung Electornics
  5. * Jonghwa Lee <jonghwa3.lee@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/slab.h>
  24. #include <linux/err.h>
  25. #include <linux/module.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/mfd/max77620.h>
  28. #include <linux/mfd/max77686.h>
  29. #include <linux/mfd/max77686-private.h>
  30. #include <linux/clk-provider.h>
  31. #include <linux/mutex.h>
  32. #include <linux/clkdev.h>
  33. #include <linux/of.h>
  34. #include <linux/regmap.h>
  35. #include <dt-bindings/clock/maxim,max77686.h>
  36. #include <dt-bindings/clock/maxim,max77802.h>
  37. #include <dt-bindings/clock/maxim,max77620.h>
  38. #define MAX77802_CLOCK_LOW_JITTER_SHIFT 0x3
  39. enum max77686_chip_name {
  40. CHIP_MAX77686,
  41. CHIP_MAX77802,
  42. CHIP_MAX77620,
  43. };
  44. struct max77686_hw_clk_info {
  45. const char *name;
  46. u32 clk_reg;
  47. u32 clk_enable_mask;
  48. u32 flags;
  49. };
  50. struct max77686_clk_init_data {
  51. struct regmap *regmap;
  52. struct clk_hw hw;
  53. struct clk_init_data clk_idata;
  54. const struct max77686_hw_clk_info *clk_info;
  55. };
  56. struct max77686_clk_driver_data {
  57. enum max77686_chip_name chip;
  58. struct max77686_clk_init_data *max_clk_data;
  59. size_t num_clks;
  60. };
  61. static const struct
  62. max77686_hw_clk_info max77686_hw_clks_info[MAX77686_CLKS_NUM] = {
  63. [MAX77686_CLK_AP] = {
  64. .name = "32khz_ap",
  65. .clk_reg = MAX77686_REG_32KHZ,
  66. .clk_enable_mask = BIT(MAX77686_CLK_AP),
  67. },
  68. [MAX77686_CLK_CP] = {
  69. .name = "32khz_cp",
  70. .clk_reg = MAX77686_REG_32KHZ,
  71. .clk_enable_mask = BIT(MAX77686_CLK_CP),
  72. },
  73. [MAX77686_CLK_PMIC] = {
  74. .name = "32khz_pmic",
  75. .clk_reg = MAX77686_REG_32KHZ,
  76. .clk_enable_mask = BIT(MAX77686_CLK_PMIC),
  77. },
  78. };
  79. static const struct
  80. max77686_hw_clk_info max77802_hw_clks_info[MAX77802_CLKS_NUM] = {
  81. [MAX77802_CLK_32K_AP] = {
  82. .name = "32khz_ap",
  83. .clk_reg = MAX77802_REG_32KHZ,
  84. .clk_enable_mask = BIT(MAX77802_CLK_32K_AP),
  85. },
  86. [MAX77802_CLK_32K_CP] = {
  87. .name = "32khz_cp",
  88. .clk_reg = MAX77802_REG_32KHZ,
  89. .clk_enable_mask = BIT(MAX77802_CLK_32K_CP),
  90. },
  91. };
  92. static const struct
  93. max77686_hw_clk_info max77620_hw_clks_info[MAX77620_CLKS_NUM] = {
  94. [MAX77620_CLK_32K_OUT0] = {
  95. .name = "32khz_out0",
  96. .clk_reg = MAX77620_REG_CNFG1_32K,
  97. .clk_enable_mask = MAX77620_CNFG1_32K_OUT0_EN,
  98. },
  99. };
  100. static struct max77686_clk_init_data *to_max77686_clk_init_data(
  101. struct clk_hw *hw)
  102. {
  103. return container_of(hw, struct max77686_clk_init_data, hw);
  104. }
  105. static int max77686_clk_prepare(struct clk_hw *hw)
  106. {
  107. struct max77686_clk_init_data *max77686 = to_max77686_clk_init_data(hw);
  108. return regmap_update_bits(max77686->regmap, max77686->clk_info->clk_reg,
  109. max77686->clk_info->clk_enable_mask,
  110. max77686->clk_info->clk_enable_mask);
  111. }
  112. static void max77686_clk_unprepare(struct clk_hw *hw)
  113. {
  114. struct max77686_clk_init_data *max77686 = to_max77686_clk_init_data(hw);
  115. regmap_update_bits(max77686->regmap, max77686->clk_info->clk_reg,
  116. max77686->clk_info->clk_enable_mask,
  117. ~max77686->clk_info->clk_enable_mask);
  118. }
  119. static int max77686_clk_is_prepared(struct clk_hw *hw)
  120. {
  121. struct max77686_clk_init_data *max77686 = to_max77686_clk_init_data(hw);
  122. int ret;
  123. u32 val;
  124. ret = regmap_read(max77686->regmap, max77686->clk_info->clk_reg, &val);
  125. if (ret < 0)
  126. return -EINVAL;
  127. return val & max77686->clk_info->clk_enable_mask;
  128. }
  129. static unsigned long max77686_recalc_rate(struct clk_hw *hw,
  130. unsigned long parent_rate)
  131. {
  132. return 32768;
  133. }
  134. static struct clk_ops max77686_clk_ops = {
  135. .prepare = max77686_clk_prepare,
  136. .unprepare = max77686_clk_unprepare,
  137. .is_prepared = max77686_clk_is_prepared,
  138. .recalc_rate = max77686_recalc_rate,
  139. };
  140. static struct clk_hw *
  141. of_clk_max77686_get(struct of_phandle_args *clkspec, void *data)
  142. {
  143. struct max77686_clk_driver_data *drv_data = data;
  144. unsigned int idx = clkspec->args[0];
  145. if (idx >= drv_data->num_clks) {
  146. pr_err("%s: invalid index %u\n", __func__, idx);
  147. return ERR_PTR(-EINVAL);
  148. }
  149. return &drv_data->max_clk_data[idx].hw;
  150. }
  151. static int max77686_clk_probe(struct platform_device *pdev)
  152. {
  153. struct device *dev = &pdev->dev;
  154. struct device *parent = dev->parent;
  155. const struct platform_device_id *id = platform_get_device_id(pdev);
  156. struct max77686_clk_driver_data *drv_data;
  157. const struct max77686_hw_clk_info *hw_clks;
  158. struct regmap *regmap;
  159. int i, ret, num_clks;
  160. drv_data = devm_kzalloc(dev, sizeof(*drv_data), GFP_KERNEL);
  161. if (!drv_data)
  162. return -ENOMEM;
  163. regmap = dev_get_regmap(parent, NULL);
  164. if (!regmap) {
  165. dev_err(dev, "Failed to get rtc regmap\n");
  166. return -ENODEV;
  167. }
  168. drv_data->chip = id->driver_data;
  169. switch (drv_data->chip) {
  170. case CHIP_MAX77686:
  171. num_clks = MAX77686_CLKS_NUM;
  172. hw_clks = max77686_hw_clks_info;
  173. break;
  174. case CHIP_MAX77802:
  175. num_clks = MAX77802_CLKS_NUM;
  176. hw_clks = max77802_hw_clks_info;
  177. break;
  178. case CHIP_MAX77620:
  179. num_clks = MAX77620_CLKS_NUM;
  180. hw_clks = max77620_hw_clks_info;
  181. break;
  182. default:
  183. dev_err(dev, "Unknown Chip ID\n");
  184. return -EINVAL;
  185. }
  186. drv_data->num_clks = num_clks;
  187. drv_data->max_clk_data = devm_kcalloc(dev, num_clks,
  188. sizeof(*drv_data->max_clk_data),
  189. GFP_KERNEL);
  190. if (!drv_data->max_clk_data)
  191. return -ENOMEM;
  192. for (i = 0; i < num_clks; i++) {
  193. struct max77686_clk_init_data *max_clk_data;
  194. const char *clk_name;
  195. max_clk_data = &drv_data->max_clk_data[i];
  196. max_clk_data->regmap = regmap;
  197. max_clk_data->clk_info = &hw_clks[i];
  198. max_clk_data->clk_idata.flags = hw_clks[i].flags;
  199. max_clk_data->clk_idata.ops = &max77686_clk_ops;
  200. if (parent->of_node &&
  201. !of_property_read_string_index(parent->of_node,
  202. "clock-output-names",
  203. i, &clk_name))
  204. max_clk_data->clk_idata.name = clk_name;
  205. else
  206. max_clk_data->clk_idata.name = hw_clks[i].name;
  207. max_clk_data->hw.init = &max_clk_data->clk_idata;
  208. ret = devm_clk_hw_register(dev, &max_clk_data->hw);
  209. if (ret) {
  210. dev_err(dev, "Failed to clock register: %d\n", ret);
  211. return ret;
  212. }
  213. ret = clk_hw_register_clkdev(&max_clk_data->hw,
  214. max_clk_data->clk_idata.name, NULL);
  215. if (ret < 0) {
  216. dev_err(dev, "Failed to clkdev register: %d\n", ret);
  217. return ret;
  218. }
  219. }
  220. if (parent->of_node) {
  221. ret = of_clk_add_hw_provider(parent->of_node, of_clk_max77686_get,
  222. drv_data);
  223. if (ret < 0) {
  224. dev_err(dev, "Failed to register OF clock provider: %d\n",
  225. ret);
  226. return ret;
  227. }
  228. }
  229. /* MAX77802: Enable low-jitter mode on the 32khz clocks. */
  230. if (drv_data->chip == CHIP_MAX77802) {
  231. ret = regmap_update_bits(regmap, MAX77802_REG_32KHZ,
  232. 1 << MAX77802_CLOCK_LOW_JITTER_SHIFT,
  233. 1 << MAX77802_CLOCK_LOW_JITTER_SHIFT);
  234. if (ret < 0) {
  235. dev_err(dev, "Failed to config low-jitter: %d\n", ret);
  236. goto remove_of_clk_provider;
  237. }
  238. }
  239. return 0;
  240. remove_of_clk_provider:
  241. if (parent->of_node)
  242. of_clk_del_provider(parent->of_node);
  243. return ret;
  244. }
  245. static int max77686_clk_remove(struct platform_device *pdev)
  246. {
  247. struct device *parent = pdev->dev.parent;
  248. if (parent->of_node)
  249. of_clk_del_provider(parent->of_node);
  250. return 0;
  251. }
  252. static const struct platform_device_id max77686_clk_id[] = {
  253. { "max77686-clk", .driver_data = CHIP_MAX77686, },
  254. { "max77802-clk", .driver_data = CHIP_MAX77802, },
  255. { "max77620-clock", .driver_data = CHIP_MAX77620, },
  256. {},
  257. };
  258. MODULE_DEVICE_TABLE(platform, max77686_clk_id);
  259. static struct platform_driver max77686_clk_driver = {
  260. .driver = {
  261. .name = "max77686-clk",
  262. },
  263. .probe = max77686_clk_probe,
  264. .remove = max77686_clk_remove,
  265. .id_table = max77686_clk_id,
  266. };
  267. module_platform_driver(max77686_clk_driver);
  268. MODULE_DESCRIPTION("MAXIM 77686 Clock Driver");
  269. MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
  270. MODULE_LICENSE("GPL");