fimc-is-i2c.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 const struct i2c_algorithm fimc_is_i2c_algorithm;
  29. static int fimc_is_i2c_probe(struct platform_device *pdev)
  30. {
  31. struct device_node *node = pdev->dev.of_node;
  32. struct fimc_is_i2c *isp_i2c;
  33. struct i2c_adapter *i2c_adap;
  34. int ret;
  35. isp_i2c = devm_kzalloc(&pdev->dev, sizeof(*isp_i2c), GFP_KERNEL);
  36. if (!isp_i2c)
  37. return -ENOMEM;
  38. isp_i2c->clock = devm_clk_get(&pdev->dev, "i2c_isp");
  39. if (IS_ERR(isp_i2c->clock)) {
  40. dev_err(&pdev->dev, "failed to get the clock\n");
  41. return PTR_ERR(isp_i2c->clock);
  42. }
  43. i2c_adap = &isp_i2c->adapter;
  44. i2c_adap->dev.of_node = node;
  45. i2c_adap->dev.parent = &pdev->dev;
  46. strlcpy(i2c_adap->name, "exynos4x12-isp-i2c", sizeof(i2c_adap->name));
  47. i2c_adap->owner = THIS_MODULE;
  48. i2c_adap->algo = &fimc_is_i2c_algorithm;
  49. i2c_adap->class = I2C_CLASS_SPD;
  50. platform_set_drvdata(pdev, isp_i2c);
  51. pm_runtime_enable(&pdev->dev);
  52. ret = i2c_add_adapter(i2c_adap);
  53. if (ret < 0)
  54. goto err_pm_dis;
  55. /*
  56. * Client drivers of this adapter don't do any I2C transfers as that
  57. * is handled by the ISP firmware. But we rely on the runtime PM
  58. * state propagation from the clients up to the adapter driver so
  59. * clear the ignore_children flags here. PM rutnime calls are not
  60. * used in probe() handler of clients of this adapter so there is
  61. * no issues with clearing the flag right after registering the I2C
  62. * adapter.
  63. */
  64. pm_suspend_ignore_children(&i2c_adap->dev, false);
  65. return 0;
  66. err_pm_dis:
  67. pm_runtime_disable(&pdev->dev);
  68. return ret;
  69. }
  70. static int fimc_is_i2c_remove(struct platform_device *pdev)
  71. {
  72. struct fimc_is_i2c *isp_i2c = platform_get_drvdata(pdev);
  73. pm_runtime_disable(&pdev->dev);
  74. i2c_del_adapter(&isp_i2c->adapter);
  75. return 0;
  76. }
  77. #ifdef CONFIG_PM
  78. static int fimc_is_i2c_runtime_suspend(struct device *dev)
  79. {
  80. struct fimc_is_i2c *isp_i2c = dev_get_drvdata(dev);
  81. clk_disable_unprepare(isp_i2c->clock);
  82. return 0;
  83. }
  84. static int fimc_is_i2c_runtime_resume(struct device *dev)
  85. {
  86. struct fimc_is_i2c *isp_i2c = dev_get_drvdata(dev);
  87. return clk_prepare_enable(isp_i2c->clock);
  88. }
  89. #endif
  90. #ifdef CONFIG_PM_SLEEP
  91. static int fimc_is_i2c_suspend(struct device *dev)
  92. {
  93. if (pm_runtime_suspended(dev))
  94. return 0;
  95. return fimc_is_i2c_runtime_suspend(dev);
  96. }
  97. static int fimc_is_i2c_resume(struct device *dev)
  98. {
  99. if (pm_runtime_suspended(dev))
  100. return 0;
  101. return fimc_is_i2c_runtime_resume(dev);
  102. }
  103. #endif
  104. static struct dev_pm_ops fimc_is_i2c_pm_ops = {
  105. SET_RUNTIME_PM_OPS(fimc_is_i2c_runtime_suspend,
  106. fimc_is_i2c_runtime_resume, NULL)
  107. SET_SYSTEM_SLEEP_PM_OPS(fimc_is_i2c_suspend, fimc_is_i2c_resume)
  108. };
  109. static const struct of_device_id fimc_is_i2c_of_match[] = {
  110. { .compatible = FIMC_IS_I2C_COMPATIBLE },
  111. { },
  112. };
  113. static struct platform_driver fimc_is_i2c_driver = {
  114. .probe = fimc_is_i2c_probe,
  115. .remove = fimc_is_i2c_remove,
  116. .driver = {
  117. .of_match_table = fimc_is_i2c_of_match,
  118. .name = "fimc-isp-i2c",
  119. .pm = &fimc_is_i2c_pm_ops,
  120. }
  121. };
  122. int fimc_is_register_i2c_driver(void)
  123. {
  124. return platform_driver_register(&fimc_is_i2c_driver);
  125. }
  126. void fimc_is_unregister_i2c_driver(void)
  127. {
  128. platform_driver_unregister(&fimc_is_i2c_driver);
  129. }