fimc-is-i2c.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/of_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. ret = i2c_add_adapter(i2c_adap);
  51. if (ret < 0) {
  52. dev_err(&pdev->dev, "failed to add I2C bus %s\n",
  53. node->full_name);
  54. return ret;
  55. }
  56. platform_set_drvdata(pdev, isp_i2c);
  57. pm_runtime_enable(&pdev->dev);
  58. pm_runtime_enable(&i2c_adap->dev);
  59. of_i2c_register_devices(i2c_adap);
  60. return 0;
  61. }
  62. static int fimc_is_i2c_remove(struct platform_device *pdev)
  63. {
  64. struct fimc_is_i2c *isp_i2c = platform_get_drvdata(pdev);
  65. pm_runtime_disable(&isp_i2c->adapter.dev);
  66. pm_runtime_disable(&pdev->dev);
  67. i2c_del_adapter(&isp_i2c->adapter);
  68. return 0;
  69. }
  70. #if defined(CONFIG_PM_RUNTIME) || defined(CONFIG_PM_SLEEP)
  71. static int fimc_is_i2c_runtime_suspend(struct device *dev)
  72. {
  73. struct fimc_is_i2c *isp_i2c = dev_get_drvdata(dev);
  74. clk_disable_unprepare(isp_i2c->clock);
  75. return 0;
  76. }
  77. static int fimc_is_i2c_runtime_resume(struct device *dev)
  78. {
  79. struct fimc_is_i2c *isp_i2c = dev_get_drvdata(dev);
  80. return clk_prepare_enable(isp_i2c->clock);
  81. }
  82. #endif
  83. #ifdef CONFIG_PM_SLEEP
  84. static int fimc_is_i2c_suspend(struct device *dev)
  85. {
  86. if (pm_runtime_suspended(dev))
  87. return 0;
  88. return fimc_is_i2c_runtime_suspend(dev);
  89. }
  90. static int fimc_is_i2c_resume(struct device *dev)
  91. {
  92. if (pm_runtime_suspended(dev))
  93. return 0;
  94. return fimc_is_i2c_runtime_resume(dev);
  95. }
  96. #endif
  97. static struct dev_pm_ops fimc_is_i2c_pm_ops = {
  98. SET_RUNTIME_PM_OPS(fimc_is_i2c_runtime_suspend,
  99. fimc_is_i2c_runtime_resume, NULL)
  100. SET_SYSTEM_SLEEP_PM_OPS(fimc_is_i2c_suspend, fimc_is_i2c_resume)
  101. };
  102. static const struct of_device_id fimc_is_i2c_of_match[] = {
  103. { .compatible = FIMC_IS_I2C_COMPATIBLE },
  104. { },
  105. };
  106. static struct platform_driver fimc_is_i2c_driver = {
  107. .probe = fimc_is_i2c_probe,
  108. .remove = fimc_is_i2c_remove,
  109. .driver = {
  110. .of_match_table = fimc_is_i2c_of_match,
  111. .name = "fimc-isp-i2c",
  112. .owner = THIS_MODULE,
  113. .pm = &fimc_is_i2c_pm_ops,
  114. }
  115. };
  116. int fimc_is_register_i2c_driver(void)
  117. {
  118. return platform_driver_register(&fimc_is_i2c_driver);
  119. }
  120. void fimc_is_unregister_i2c_driver(void)
  121. {
  122. platform_driver_unregister(&fimc_is_i2c_driver);
  123. }