clk-max77686.c 5.5 KB

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