i2c-mux-ltc4306.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Linear Technology LTC4306 and LTC4305 I2C multiplexer/switch
  3. *
  4. * Copyright (C) 2017 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2.
  7. *
  8. * Based on: i2c-mux-pca954x.c
  9. *
  10. * Datasheet: http://cds.linear.com/docs/en/datasheet/4306.pdf
  11. */
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/gpio/driver.h>
  14. #include <linux/i2c-mux.h>
  15. #include <linux/i2c.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/of_device.h>
  19. #include <linux/property.h>
  20. #include <linux/regmap.h>
  21. #include <linux/slab.h>
  22. #define LTC4305_MAX_NCHANS 2
  23. #define LTC4306_MAX_NCHANS 4
  24. #define LTC_REG_STATUS 0x0
  25. #define LTC_REG_CONFIG 0x1
  26. #define LTC_REG_MODE 0x2
  27. #define LTC_REG_SWITCH 0x3
  28. #define LTC_DOWNSTREAM_ACCL_EN BIT(6)
  29. #define LTC_UPSTREAM_ACCL_EN BIT(7)
  30. #define LTC_GPIO_ALL_INPUT 0xC0
  31. #define LTC_SWITCH_MASK 0xF0
  32. enum ltc_type {
  33. ltc_4305,
  34. ltc_4306,
  35. };
  36. struct chip_desc {
  37. u8 nchans;
  38. u8 num_gpios;
  39. };
  40. struct ltc4306 {
  41. struct regmap *regmap;
  42. struct gpio_chip gpiochip;
  43. const struct chip_desc *chip;
  44. };
  45. static const struct chip_desc chips[] = {
  46. [ltc_4305] = {
  47. .nchans = LTC4305_MAX_NCHANS,
  48. },
  49. [ltc_4306] = {
  50. .nchans = LTC4306_MAX_NCHANS,
  51. .num_gpios = 2,
  52. },
  53. };
  54. static bool ltc4306_is_volatile_reg(struct device *dev, unsigned int reg)
  55. {
  56. return (reg == LTC_REG_CONFIG) ? true : false;
  57. }
  58. static const struct regmap_config ltc4306_regmap_config = {
  59. .reg_bits = 8,
  60. .val_bits = 8,
  61. .max_register = LTC_REG_SWITCH,
  62. .volatile_reg = ltc4306_is_volatile_reg,
  63. .cache_type = REGCACHE_FLAT,
  64. };
  65. static int ltc4306_gpio_get(struct gpio_chip *chip, unsigned int offset)
  66. {
  67. struct ltc4306 *data = gpiochip_get_data(chip);
  68. unsigned int val;
  69. int ret;
  70. ret = regmap_read(data->regmap, LTC_REG_CONFIG, &val);
  71. if (ret < 0)
  72. return ret;
  73. return !!(val & BIT(1 - offset));
  74. }
  75. static void ltc4306_gpio_set(struct gpio_chip *chip, unsigned int offset,
  76. int value)
  77. {
  78. struct ltc4306 *data = gpiochip_get_data(chip);
  79. regmap_update_bits(data->regmap, LTC_REG_CONFIG, BIT(5 - offset),
  80. value ? BIT(5 - offset) : 0);
  81. }
  82. static int ltc4306_gpio_get_direction(struct gpio_chip *chip,
  83. unsigned int offset)
  84. {
  85. struct ltc4306 *data = gpiochip_get_data(chip);
  86. unsigned int val;
  87. int ret;
  88. ret = regmap_read(data->regmap, LTC_REG_MODE, &val);
  89. if (ret < 0)
  90. return ret;
  91. return !!(val & BIT(7 - offset));
  92. }
  93. static int ltc4306_gpio_direction_input(struct gpio_chip *chip,
  94. unsigned int offset)
  95. {
  96. struct ltc4306 *data = gpiochip_get_data(chip);
  97. return regmap_update_bits(data->regmap, LTC_REG_MODE,
  98. BIT(7 - offset), BIT(7 - offset));
  99. }
  100. static int ltc4306_gpio_direction_output(struct gpio_chip *chip,
  101. unsigned int offset, int value)
  102. {
  103. struct ltc4306 *data = gpiochip_get_data(chip);
  104. ltc4306_gpio_set(chip, offset, value);
  105. return regmap_update_bits(data->regmap, LTC_REG_MODE,
  106. BIT(7 - offset), 0);
  107. }
  108. static int ltc4306_gpio_set_config(struct gpio_chip *chip,
  109. unsigned int offset, unsigned long config)
  110. {
  111. struct ltc4306 *data = gpiochip_get_data(chip);
  112. unsigned int val;
  113. switch (pinconf_to_config_param(config)) {
  114. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  115. val = 0;
  116. break;
  117. case PIN_CONFIG_DRIVE_PUSH_PULL:
  118. val = BIT(4 - offset);
  119. break;
  120. default:
  121. return -ENOTSUPP;
  122. }
  123. return regmap_update_bits(data->regmap, LTC_REG_MODE,
  124. BIT(4 - offset), val);
  125. }
  126. static int ltc4306_gpio_init(struct ltc4306 *data)
  127. {
  128. struct device *dev = regmap_get_device(data->regmap);
  129. if (!data->chip->num_gpios)
  130. return 0;
  131. data->gpiochip.label = dev_name(dev);
  132. data->gpiochip.base = -1;
  133. data->gpiochip.ngpio = data->chip->num_gpios;
  134. data->gpiochip.parent = dev;
  135. data->gpiochip.can_sleep = true;
  136. data->gpiochip.get_direction = ltc4306_gpio_get_direction;
  137. data->gpiochip.direction_input = ltc4306_gpio_direction_input;
  138. data->gpiochip.direction_output = ltc4306_gpio_direction_output;
  139. data->gpiochip.get = ltc4306_gpio_get;
  140. data->gpiochip.set = ltc4306_gpio_set;
  141. data->gpiochip.set_config = ltc4306_gpio_set_config;
  142. data->gpiochip.owner = THIS_MODULE;
  143. /* gpiolib assumes all GPIOs default input */
  144. regmap_write(data->regmap, LTC_REG_MODE, LTC_GPIO_ALL_INPUT);
  145. return devm_gpiochip_add_data(dev, &data->gpiochip, data);
  146. }
  147. static int ltc4306_select_mux(struct i2c_mux_core *muxc, u32 chan)
  148. {
  149. struct ltc4306 *data = i2c_mux_priv(muxc);
  150. return regmap_update_bits(data->regmap, LTC_REG_SWITCH,
  151. LTC_SWITCH_MASK, BIT(7 - chan));
  152. }
  153. static int ltc4306_deselect_mux(struct i2c_mux_core *muxc, u32 chan)
  154. {
  155. struct ltc4306 *data = i2c_mux_priv(muxc);
  156. return regmap_update_bits(data->regmap, LTC_REG_SWITCH,
  157. LTC_SWITCH_MASK, 0);
  158. }
  159. static const struct i2c_device_id ltc4306_id[] = {
  160. { "ltc4305", ltc_4305 },
  161. { "ltc4306", ltc_4306 },
  162. { }
  163. };
  164. MODULE_DEVICE_TABLE(i2c, ltc4306_id);
  165. static const struct of_device_id ltc4306_of_match[] = {
  166. { .compatible = "lltc,ltc4305", .data = &chips[ltc_4305] },
  167. { .compatible = "lltc,ltc4306", .data = &chips[ltc_4306] },
  168. { }
  169. };
  170. MODULE_DEVICE_TABLE(of, ltc4306_of_match);
  171. static int ltc4306_probe(struct i2c_client *client)
  172. {
  173. struct i2c_adapter *adap = to_i2c_adapter(client->dev.parent);
  174. const struct chip_desc *chip;
  175. struct i2c_mux_core *muxc;
  176. struct ltc4306 *data;
  177. struct gpio_desc *gpio;
  178. bool idle_disc;
  179. unsigned int val = 0;
  180. int num, ret;
  181. chip = of_device_get_match_data(&client->dev);
  182. if (!chip)
  183. chip = &chips[i2c_match_id(ltc4306_id, client)->driver_data];
  184. idle_disc = device_property_read_bool(&client->dev,
  185. "i2c-mux-idle-disconnect");
  186. muxc = i2c_mux_alloc(adap, &client->dev,
  187. chip->nchans, sizeof(*data),
  188. I2C_MUX_LOCKED, ltc4306_select_mux,
  189. idle_disc ? ltc4306_deselect_mux : NULL);
  190. if (!muxc)
  191. return -ENOMEM;
  192. data = i2c_mux_priv(muxc);
  193. data->chip = chip;
  194. i2c_set_clientdata(client, muxc);
  195. data->regmap = devm_regmap_init_i2c(client, &ltc4306_regmap_config);
  196. if (IS_ERR(data->regmap)) {
  197. ret = PTR_ERR(data->regmap);
  198. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  199. ret);
  200. return ret;
  201. }
  202. /* Reset and enable the mux if an enable GPIO is specified. */
  203. gpio = devm_gpiod_get_optional(&client->dev, "enable", GPIOD_OUT_LOW);
  204. if (IS_ERR(gpio))
  205. return PTR_ERR(gpio);
  206. if (gpio) {
  207. udelay(1);
  208. gpiod_set_value(gpio, 1);
  209. }
  210. /*
  211. * Write the mux register at addr to verify
  212. * that the mux is in fact present. This also
  213. * initializes the mux to disconnected state.
  214. */
  215. if (regmap_write(data->regmap, LTC_REG_SWITCH, 0) < 0) {
  216. dev_warn(&client->dev, "probe failed\n");
  217. return -ENODEV;
  218. }
  219. if (device_property_read_bool(&client->dev,
  220. "ltc,downstream-accelerators-enable"))
  221. val |= LTC_DOWNSTREAM_ACCL_EN;
  222. if (device_property_read_bool(&client->dev,
  223. "ltc,upstream-accelerators-enable"))
  224. val |= LTC_UPSTREAM_ACCL_EN;
  225. if (regmap_write(data->regmap, LTC_REG_CONFIG, val) < 0)
  226. return -ENODEV;
  227. ret = ltc4306_gpio_init(data);
  228. if (ret < 0)
  229. return ret;
  230. /* Now create an adapter for each channel */
  231. for (num = 0; num < chip->nchans; num++) {
  232. ret = i2c_mux_add_adapter(muxc, 0, num, 0);
  233. if (ret) {
  234. i2c_mux_del_adapters(muxc);
  235. return ret;
  236. }
  237. }
  238. dev_info(&client->dev,
  239. "registered %d multiplexed busses for I2C switch %s\n",
  240. num, client->name);
  241. return 0;
  242. }
  243. static int ltc4306_remove(struct i2c_client *client)
  244. {
  245. struct i2c_mux_core *muxc = i2c_get_clientdata(client);
  246. i2c_mux_del_adapters(muxc);
  247. return 0;
  248. }
  249. static struct i2c_driver ltc4306_driver = {
  250. .driver = {
  251. .name = "ltc4306",
  252. .of_match_table = of_match_ptr(ltc4306_of_match),
  253. },
  254. .probe_new = ltc4306_probe,
  255. .remove = ltc4306_remove,
  256. .id_table = ltc4306_id,
  257. };
  258. module_i2c_driver(ltc4306_driver);
  259. MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
  260. MODULE_DESCRIPTION("Linear Technology LTC4306, LTC4305 I2C mux/switch driver");
  261. MODULE_LICENSE("GPL v2");