clk-max77686.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * clk-max77686.c - Clock driver for Maxim 77686
  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/platform_device.h>
  26. #include <linux/mfd/max77686.h>
  27. #include <linux/mfd/max77686-private.h>
  28. #include <linux/clk-provider.h>
  29. #include <linux/mutex.h>
  30. #include <linux/clkdev.h>
  31. enum {
  32. MAX77686_CLK_AP = 0,
  33. MAX77686_CLK_CP,
  34. MAX77686_CLK_PMIC,
  35. MAX77686_CLKS_NUM,
  36. };
  37. struct max77686_clk {
  38. struct max77686_dev *iodev;
  39. u32 mask;
  40. struct clk_hw hw;
  41. struct clk_lookup *lookup;
  42. };
  43. static struct max77686_clk *get_max77686_clk(struct clk_hw *hw)
  44. {
  45. return container_of(hw, struct max77686_clk, hw);
  46. }
  47. static int max77686_clk_prepare(struct clk_hw *hw)
  48. {
  49. struct max77686_clk *max77686;
  50. int ret;
  51. max77686 = get_max77686_clk(hw);
  52. if (!max77686)
  53. return -ENOMEM;
  54. ret = regmap_update_bits(max77686->iodev->regmap,
  55. MAX77686_REG_32KHZ, max77686->mask, max77686->mask);
  56. return ret;
  57. }
  58. static void max77686_clk_unprepare(struct clk_hw *hw)
  59. {
  60. struct max77686_clk *max77686;
  61. max77686 = get_max77686_clk(hw);
  62. if (!max77686)
  63. return;
  64. regmap_update_bits(max77686->iodev->regmap,
  65. MAX77686_REG_32KHZ, max77686->mask, ~max77686->mask);
  66. }
  67. static int max77686_clk_is_enabled(struct clk_hw *hw)
  68. {
  69. struct max77686_clk *max77686;
  70. int ret;
  71. u32 val;
  72. max77686 = get_max77686_clk(hw);
  73. if (!max77686)
  74. return -ENOMEM;
  75. ret = regmap_read(max77686->iodev->regmap,
  76. MAX77686_REG_32KHZ, &val);
  77. if (ret < 0)
  78. return -EINVAL;
  79. return val & max77686->mask;
  80. }
  81. static struct clk_ops max77686_clk_ops = {
  82. .prepare = max77686_clk_prepare,
  83. .unprepare = max77686_clk_unprepare,
  84. .is_enabled = max77686_clk_is_enabled,
  85. };
  86. static struct clk_init_data max77686_clks_init[MAX77686_CLKS_NUM] = {
  87. [MAX77686_CLK_AP] = {
  88. .name = "32khz_ap",
  89. .ops = &max77686_clk_ops,
  90. .flags = CLK_IS_ROOT,
  91. },
  92. [MAX77686_CLK_CP] = {
  93. .name = "32khz_cp",
  94. .ops = &max77686_clk_ops,
  95. .flags = CLK_IS_ROOT,
  96. },
  97. [MAX77686_CLK_PMIC] = {
  98. .name = "32khz_pmic",
  99. .ops = &max77686_clk_ops,
  100. .flags = CLK_IS_ROOT,
  101. },
  102. };
  103. static int max77686_clk_register(struct device *dev,
  104. struct max77686_clk *max77686)
  105. {
  106. struct clk *clk;
  107. struct clk_hw *hw = &max77686->hw;
  108. clk = clk_register(dev, hw);
  109. if (IS_ERR(clk))
  110. return -ENOMEM;
  111. max77686->lookup = devm_kzalloc(dev, sizeof(struct clk_lookup),
  112. GFP_KERNEL);
  113. if (IS_ERR(max77686->lookup))
  114. return -ENOMEM;
  115. max77686->lookup->con_id = hw->init->name;
  116. max77686->lookup->clk = clk;
  117. clkdev_add(max77686->lookup);
  118. return 0;
  119. }
  120. static int max77686_clk_probe(struct platform_device *pdev)
  121. {
  122. struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  123. struct max77686_clk **max77686_clks;
  124. int i, ret;
  125. max77686_clks = devm_kzalloc(&pdev->dev, sizeof(struct max77686_clk *)
  126. * MAX77686_CLKS_NUM, GFP_KERNEL);
  127. if (IS_ERR(max77686_clks))
  128. return -ENOMEM;
  129. for (i = 0; i < MAX77686_CLKS_NUM; i++) {
  130. max77686_clks[i] = devm_kzalloc(&pdev->dev,
  131. sizeof(struct max77686_clk), GFP_KERNEL);
  132. if (IS_ERR(max77686_clks[i]))
  133. return -ENOMEM;
  134. }
  135. for (i = 0; i < MAX77686_CLKS_NUM; i++) {
  136. max77686_clks[i]->iodev = iodev;
  137. max77686_clks[i]->mask = 1 << i;
  138. max77686_clks[i]->hw.init = &max77686_clks_init[i];
  139. ret = max77686_clk_register(&pdev->dev, max77686_clks[i]);
  140. if (ret) {
  141. switch (i) {
  142. case MAX77686_CLK_AP:
  143. dev_err(&pdev->dev, "Fail to register CLK_AP\n");
  144. goto err_clk_ap;
  145. break;
  146. case MAX77686_CLK_CP:
  147. dev_err(&pdev->dev, "Fail to register CLK_CP\n");
  148. goto err_clk_cp;
  149. break;
  150. case MAX77686_CLK_PMIC:
  151. dev_err(&pdev->dev, "Fail to register CLK_PMIC\n");
  152. goto err_clk_pmic;
  153. }
  154. }
  155. }
  156. platform_set_drvdata(pdev, max77686_clks);
  157. goto out;
  158. err_clk_pmic:
  159. clkdev_drop(max77686_clks[MAX77686_CLK_CP]->lookup);
  160. kfree(max77686_clks[MAX77686_CLK_CP]->hw.clk);
  161. err_clk_cp:
  162. clkdev_drop(max77686_clks[MAX77686_CLK_AP]->lookup);
  163. kfree(max77686_clks[MAX77686_CLK_AP]->hw.clk);
  164. err_clk_ap:
  165. out:
  166. return ret;
  167. }
  168. static int max77686_clk_remove(struct platform_device *pdev)
  169. {
  170. struct max77686_clk **max77686_clks = platform_get_drvdata(pdev);
  171. int i;
  172. for (i = 0; i < MAX77686_CLKS_NUM; i++) {
  173. clkdev_drop(max77686_clks[i]->lookup);
  174. kfree(max77686_clks[i]->hw.clk);
  175. }
  176. return 0;
  177. }
  178. static const struct platform_device_id max77686_clk_id[] = {
  179. { "max77686-clk", 0},
  180. { },
  181. };
  182. MODULE_DEVICE_TABLE(platform, max77686_clk_id);
  183. static struct platform_driver max77686_clk_driver = {
  184. .driver = {
  185. .name = "max77686-clk",
  186. .owner = THIS_MODULE,
  187. },
  188. .probe = max77686_clk_probe,
  189. .remove = max77686_clk_remove,
  190. .id_table = max77686_clk_id,
  191. };
  192. static int __init max77686_clk_init(void)
  193. {
  194. return platform_driver_register(&max77686_clk_driver);
  195. }
  196. subsys_initcall(max77686_clk_init);
  197. static void __init max77686_clk_cleanup(void)
  198. {
  199. platform_driver_unregister(&max77686_clk_driver);
  200. }
  201. module_exit(max77686_clk_cleanup);
  202. MODULE_DESCRIPTION("MAXIM 77686 Clock Driver");
  203. MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
  204. MODULE_LICENSE("GPL");