fimc-is-i2c.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. 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. return 0;
  60. }
  61. static int fimc_is_i2c_remove(struct platform_device *pdev)
  62. {
  63. struct fimc_is_i2c *isp_i2c = platform_get_drvdata(pdev);
  64. pm_runtime_disable(&isp_i2c->adapter.dev);
  65. pm_runtime_disable(&pdev->dev);
  66. i2c_del_adapter(&isp_i2c->adapter);
  67. return 0;
  68. }
  69. #ifdef CONFIG_PM
  70. static int fimc_is_i2c_runtime_suspend(struct device *dev)
  71. {
  72. struct fimc_is_i2c *isp_i2c = dev_get_drvdata(dev);
  73. clk_disable_unprepare(isp_i2c->clock);
  74. return 0;
  75. }
  76. static int fimc_is_i2c_runtime_resume(struct device *dev)
  77. {
  78. struct fimc_is_i2c *isp_i2c = dev_get_drvdata(dev);
  79. return clk_prepare_enable(isp_i2c->clock);
  80. }
  81. #endif
  82. #ifdef CONFIG_PM_SLEEP
  83. static int fimc_is_i2c_suspend(struct device *dev)
  84. {
  85. if (pm_runtime_suspended(dev))
  86. return 0;
  87. return fimc_is_i2c_runtime_suspend(dev);
  88. }
  89. static int fimc_is_i2c_resume(struct device *dev)
  90. {
  91. if (pm_runtime_suspended(dev))
  92. return 0;
  93. return fimc_is_i2c_runtime_resume(dev);
  94. }
  95. #endif
  96. static struct dev_pm_ops fimc_is_i2c_pm_ops = {
  97. SET_RUNTIME_PM_OPS(fimc_is_i2c_runtime_suspend,
  98. fimc_is_i2c_runtime_resume, NULL)
  99. SET_SYSTEM_SLEEP_PM_OPS(fimc_is_i2c_suspend, fimc_is_i2c_resume)
  100. };
  101. static const struct of_device_id fimc_is_i2c_of_match[] = {
  102. { .compatible = FIMC_IS_I2C_COMPATIBLE },
  103. { },
  104. };
  105. static struct platform_driver fimc_is_i2c_driver = {
  106. .probe = fimc_is_i2c_probe,
  107. .remove = fimc_is_i2c_remove,
  108. .driver = {
  109. .of_match_table = fimc_is_i2c_of_match,
  110. .name = "fimc-isp-i2c",
  111. .pm = &fimc_is_i2c_pm_ops,
  112. }
  113. };
  114. int fimc_is_register_i2c_driver(void)
  115. {
  116. return platform_driver_register(&fimc_is_i2c_driver);
  117. }
  118. void fimc_is_unregister_i2c_driver(void)
  119. {
  120. platform_driver_unregister(&fimc_is_i2c_driver);
  121. }