fimc-is-i2c.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Samsung EXYNOS4x12 FIMC-IS (Imaging Subsystem) driver
  3. *
  4. * Copyright (C) 2013 Samsung Electronics Co., Ltd.
  5. *
  6. * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/module.h>
  14. #include <linux/i2c.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/slab.h>
  18. #include "fimc-is-i2c.h"
  19. struct fimc_is_i2c {
  20. struct i2c_adapter adapter;
  21. struct clk *clock;
  22. };
  23. /*
  24. * An empty algorithm is used as the actual I2C bus controller driver
  25. * is implemented in the FIMC-IS subsystem firmware and the host CPU
  26. * doesn't access the I2C bus controller.
  27. */
  28. static u32 is_i2c_func(struct i2c_adapter *adap)
  29. {
  30. return I2C_FUNC_I2C;
  31. }
  32. static const struct i2c_algorithm fimc_is_i2c_algorithm = {
  33. .functionality = is_i2c_func,
  34. };
  35. static int fimc_is_i2c_probe(struct platform_device *pdev)
  36. {
  37. struct device_node *node = pdev->dev.of_node;
  38. struct fimc_is_i2c *isp_i2c;
  39. struct i2c_adapter *i2c_adap;
  40. int ret;
  41. isp_i2c = devm_kzalloc(&pdev->dev, sizeof(*isp_i2c), GFP_KERNEL);
  42. if (!isp_i2c)
  43. return -ENOMEM;
  44. isp_i2c->clock = devm_clk_get(&pdev->dev, "i2c_isp");
  45. if (IS_ERR(isp_i2c->clock)) {
  46. dev_err(&pdev->dev, "failed to get the clock\n");
  47. return PTR_ERR(isp_i2c->clock);
  48. }
  49. i2c_adap = &isp_i2c->adapter;
  50. i2c_adap->dev.of_node = node;
  51. i2c_adap->dev.parent = &pdev->dev;
  52. strlcpy(i2c_adap->name, "exynos4x12-isp-i2c", sizeof(i2c_adap->name));
  53. i2c_adap->owner = THIS_MODULE;
  54. i2c_adap->algo = &fimc_is_i2c_algorithm;
  55. i2c_adap->class = I2C_CLASS_SPD;
  56. platform_set_drvdata(pdev, isp_i2c);
  57. pm_runtime_enable(&pdev->dev);
  58. ret = i2c_add_adapter(i2c_adap);
  59. if (ret < 0)
  60. goto err_pm_dis;
  61. /*
  62. * Client drivers of this adapter don't do any I2C transfers as that
  63. * is handled by the ISP firmware. But we rely on the runtime PM
  64. * state propagation from the clients up to the adapter driver so
  65. * clear the ignore_children flags here. PM rutnime calls are not
  66. * used in probe() handler of clients of this adapter so there is
  67. * no issues with clearing the flag right after registering the I2C
  68. * adapter.
  69. */
  70. pm_suspend_ignore_children(&i2c_adap->dev, false);
  71. return 0;
  72. err_pm_dis:
  73. pm_runtime_disable(&pdev->dev);
  74. return ret;
  75. }
  76. static int fimc_is_i2c_remove(struct platform_device *pdev)
  77. {
  78. struct fimc_is_i2c *isp_i2c = platform_get_drvdata(pdev);
  79. pm_runtime_disable(&pdev->dev);
  80. i2c_del_adapter(&isp_i2c->adapter);
  81. return 0;
  82. }
  83. #ifdef CONFIG_PM
  84. static int fimc_is_i2c_runtime_suspend(struct device *dev)
  85. {
  86. struct fimc_is_i2c *isp_i2c = dev_get_drvdata(dev);
  87. clk_disable_unprepare(isp_i2c->clock);
  88. return 0;
  89. }
  90. static int fimc_is_i2c_runtime_resume(struct device *dev)
  91. {
  92. struct fimc_is_i2c *isp_i2c = dev_get_drvdata(dev);
  93. return clk_prepare_enable(isp_i2c->clock);
  94. }
  95. #endif
  96. #ifdef CONFIG_PM_SLEEP
  97. static int fimc_is_i2c_suspend(struct device *dev)
  98. {
  99. if (pm_runtime_suspended(dev))
  100. return 0;
  101. return fimc_is_i2c_runtime_suspend(dev);
  102. }
  103. static int fimc_is_i2c_resume(struct device *dev)
  104. {
  105. if (pm_runtime_suspended(dev))
  106. return 0;
  107. return fimc_is_i2c_runtime_resume(dev);
  108. }
  109. #endif
  110. static const struct dev_pm_ops fimc_is_i2c_pm_ops = {
  111. SET_RUNTIME_PM_OPS(fimc_is_i2c_runtime_suspend,
  112. fimc_is_i2c_runtime_resume, NULL)
  113. SET_SYSTEM_SLEEP_PM_OPS(fimc_is_i2c_suspend, fimc_is_i2c_resume)
  114. };
  115. static const struct of_device_id fimc_is_i2c_of_match[] = {
  116. { .compatible = FIMC_IS_I2C_COMPATIBLE },
  117. { },
  118. };
  119. static struct platform_driver fimc_is_i2c_driver = {
  120. .probe = fimc_is_i2c_probe,
  121. .remove = fimc_is_i2c_remove,
  122. .driver = {
  123. .of_match_table = fimc_is_i2c_of_match,
  124. .name = "fimc-isp-i2c",
  125. .pm = &fimc_is_i2c_pm_ops,
  126. }
  127. };
  128. int fimc_is_register_i2c_driver(void)
  129. {
  130. return platform_driver_register(&fimc_is_i2c_driver);
  131. }
  132. void fimc_is_unregister_i2c_driver(void)
  133. {
  134. platform_driver_unregister(&fimc_is_i2c_driver);
  135. }