i2c-mux-gpio.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * I2C multiplexer using GPIO API
  3. *
  4. * Peter Korsgaard <peter.korsgaard@barco.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/i2c.h>
  11. #include <linux/i2c-mux.h>
  12. #include <linux/i2c-mux-gpio.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/gpio.h>
  18. struct gpiomux {
  19. struct i2c_adapter *parent;
  20. struct i2c_adapter **adap; /* child busses */
  21. struct i2c_mux_gpio_platform_data data;
  22. unsigned gpio_base;
  23. };
  24. static void i2c_mux_gpio_set(const struct gpiomux *mux, unsigned val)
  25. {
  26. int i;
  27. for (i = 0; i < mux->data.n_gpios; i++)
  28. gpio_set_value(mux->gpio_base + mux->data.gpios[i],
  29. val & (1 << i));
  30. }
  31. static int i2c_mux_gpio_select(struct i2c_adapter *adap, void *data, u32 chan)
  32. {
  33. struct gpiomux *mux = data;
  34. i2c_mux_gpio_set(mux, mux->data.values[chan]);
  35. return 0;
  36. }
  37. static int i2c_mux_gpio_deselect(struct i2c_adapter *adap, void *data, u32 chan)
  38. {
  39. struct gpiomux *mux = data;
  40. i2c_mux_gpio_set(mux, mux->data.idle);
  41. return 0;
  42. }
  43. static int __devinit match_gpio_chip_by_label(struct gpio_chip *chip,
  44. void *data)
  45. {
  46. return !strcmp(chip->label, data);
  47. }
  48. static int __devinit i2c_mux_gpio_probe(struct platform_device *pdev)
  49. {
  50. struct gpiomux *mux;
  51. struct i2c_mux_gpio_platform_data *pdata;
  52. struct i2c_adapter *parent;
  53. int (*deselect) (struct i2c_adapter *, void *, u32);
  54. unsigned initial_state, gpio_base;
  55. int i, ret;
  56. pdata = pdev->dev.platform_data;
  57. if (!pdata) {
  58. dev_err(&pdev->dev, "Missing platform data\n");
  59. return -ENODEV;
  60. }
  61. /*
  62. * If a GPIO chip name is provided, the GPIO pin numbers provided are
  63. * relative to its base GPIO number. Otherwise they are absolute.
  64. */
  65. if (pdata->gpio_chip) {
  66. struct gpio_chip *gpio;
  67. gpio = gpiochip_find(pdata->gpio_chip,
  68. match_gpio_chip_by_label);
  69. if (!gpio)
  70. return -EPROBE_DEFER;
  71. gpio_base = gpio->base;
  72. } else {
  73. gpio_base = 0;
  74. }
  75. parent = i2c_get_adapter(pdata->parent);
  76. if (!parent) {
  77. dev_err(&pdev->dev, "Parent adapter (%d) not found\n",
  78. pdata->parent);
  79. return -ENODEV;
  80. }
  81. mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
  82. if (!mux) {
  83. ret = -ENOMEM;
  84. goto alloc_failed;
  85. }
  86. mux->parent = parent;
  87. mux->data = *pdata;
  88. mux->gpio_base = gpio_base;
  89. mux->adap = devm_kzalloc(&pdev->dev,
  90. sizeof(*mux->adap) * pdata->n_values,
  91. GFP_KERNEL);
  92. if (!mux->adap) {
  93. ret = -ENOMEM;
  94. goto alloc_failed;
  95. }
  96. if (pdata->idle != I2C_MUX_GPIO_NO_IDLE) {
  97. initial_state = pdata->idle;
  98. deselect = i2c_mux_gpio_deselect;
  99. } else {
  100. initial_state = pdata->values[0];
  101. deselect = NULL;
  102. }
  103. for (i = 0; i < pdata->n_gpios; i++) {
  104. ret = gpio_request(gpio_base + pdata->gpios[i], "i2c-mux-gpio");
  105. if (ret)
  106. goto err_request_gpio;
  107. gpio_direction_output(gpio_base + pdata->gpios[i],
  108. initial_state & (1 << i));
  109. }
  110. for (i = 0; i < pdata->n_values; i++) {
  111. u32 nr = pdata->base_nr ? (pdata->base_nr + i) : 0;
  112. unsigned int class = pdata->classes ? pdata->classes[i] : 0;
  113. mux->adap[i] = i2c_add_mux_adapter(parent, &pdev->dev, mux, nr,
  114. i, class,
  115. i2c_mux_gpio_select, deselect);
  116. if (!mux->adap[i]) {
  117. ret = -ENODEV;
  118. dev_err(&pdev->dev, "Failed to add adapter %d\n", i);
  119. goto add_adapter_failed;
  120. }
  121. }
  122. dev_info(&pdev->dev, "%d port mux on %s adapter\n",
  123. pdata->n_values, parent->name);
  124. platform_set_drvdata(pdev, mux);
  125. return 0;
  126. add_adapter_failed:
  127. for (; i > 0; i--)
  128. i2c_del_mux_adapter(mux->adap[i - 1]);
  129. i = pdata->n_gpios;
  130. err_request_gpio:
  131. for (; i > 0; i--)
  132. gpio_free(gpio_base + pdata->gpios[i - 1]);
  133. alloc_failed:
  134. i2c_put_adapter(parent);
  135. return ret;
  136. }
  137. static int __devexit i2c_mux_gpio_remove(struct platform_device *pdev)
  138. {
  139. struct gpiomux *mux = platform_get_drvdata(pdev);
  140. int i;
  141. for (i = 0; i < mux->data.n_values; i++)
  142. i2c_del_mux_adapter(mux->adap[i]);
  143. for (i = 0; i < mux->data.n_gpios; i++)
  144. gpio_free(mux->gpio_base + mux->data.gpios[i]);
  145. platform_set_drvdata(pdev, NULL);
  146. i2c_put_adapter(mux->parent);
  147. return 0;
  148. }
  149. static struct platform_driver i2c_mux_gpio_driver = {
  150. .probe = i2c_mux_gpio_probe,
  151. .remove = __devexit_p(i2c_mux_gpio_remove),
  152. .driver = {
  153. .owner = THIS_MODULE,
  154. .name = "i2c-mux-gpio",
  155. },
  156. };
  157. module_platform_driver(i2c_mux_gpio_driver);
  158. MODULE_DESCRIPTION("GPIO-based I2C multiplexer driver");
  159. MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard@barco.com>");
  160. MODULE_LICENSE("GPL");
  161. MODULE_ALIAS("platform:i2c-mux-gpio");