i2c-mux-ltc4306.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. const struct i2c_device_id *id)
  173. {
  174. struct i2c_adapter *adap = to_i2c_adapter(client->dev.parent);
  175. const struct chip_desc *chip;
  176. struct i2c_mux_core *muxc;
  177. struct ltc4306 *data;
  178. struct gpio_desc *gpio;
  179. bool idle_disc;
  180. unsigned int val = 0;
  181. int num, ret;
  182. chip = of_device_get_match_data(&client->dev);
  183. if (!chip)
  184. chip = &chips[id->driver_data];
  185. idle_disc = device_property_read_bool(&client->dev,
  186. "i2c-mux-idle-disconnect");
  187. muxc = i2c_mux_alloc(adap, &client->dev,
  188. chip->nchans, sizeof(*data),
  189. I2C_MUX_LOCKED, ltc4306_select_mux,
  190. idle_disc ? ltc4306_deselect_mux : NULL);
  191. if (!muxc)
  192. return -ENOMEM;
  193. data = i2c_mux_priv(muxc);
  194. data->chip = chip;
  195. i2c_set_clientdata(client, muxc);
  196. data->regmap = devm_regmap_init_i2c(client, &ltc4306_regmap_config);
  197. if (IS_ERR(data->regmap)) {
  198. ret = PTR_ERR(data->regmap);
  199. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  200. ret);
  201. return ret;
  202. }
  203. /* Reset and enable the mux if an enable GPIO is specified. */
  204. gpio = devm_gpiod_get_optional(&client->dev, "enable", GPIOD_OUT_LOW);
  205. if (IS_ERR(gpio))
  206. return PTR_ERR(gpio);
  207. if (gpio) {
  208. udelay(1);
  209. gpiod_set_value(gpio, 1);
  210. }
  211. /*
  212. * Write the mux register at addr to verify
  213. * that the mux is in fact present. This also
  214. * initializes the mux to disconnected state.
  215. */
  216. if (regmap_write(data->regmap, LTC_REG_SWITCH, 0) < 0) {
  217. dev_warn(&client->dev, "probe failed\n");
  218. return -ENODEV;
  219. }
  220. if (device_property_read_bool(&client->dev,
  221. "ltc,downstream-accelerators-enable"))
  222. val |= LTC_DOWNSTREAM_ACCL_EN;
  223. if (device_property_read_bool(&client->dev,
  224. "ltc,upstream-accelerators-enable"))
  225. val |= LTC_UPSTREAM_ACCL_EN;
  226. if (regmap_write(data->regmap, LTC_REG_CONFIG, val) < 0)
  227. return -ENODEV;
  228. ret = ltc4306_gpio_init(data);
  229. if (ret < 0)
  230. return ret;
  231. /* Now create an adapter for each channel */
  232. for (num = 0; num < chip->nchans; num++) {
  233. ret = i2c_mux_add_adapter(muxc, 0, num, 0);
  234. if (ret) {
  235. i2c_mux_del_adapters(muxc);
  236. return ret;
  237. }
  238. }
  239. dev_info(&client->dev,
  240. "registered %d multiplexed busses for I2C switch %s\n",
  241. num, client->name);
  242. return 0;
  243. }
  244. static int ltc4306_remove(struct i2c_client *client)
  245. {
  246. struct i2c_mux_core *muxc = i2c_get_clientdata(client);
  247. i2c_mux_del_adapters(muxc);
  248. return 0;
  249. }
  250. static struct i2c_driver ltc4306_driver = {
  251. .driver = {
  252. .name = "ltc4306",
  253. .of_match_table = of_match_ptr(ltc4306_of_match),
  254. },
  255. .probe = ltc4306_probe,
  256. .remove = ltc4306_remove,
  257. .id_table = ltc4306_id,
  258. };
  259. module_i2c_driver(ltc4306_driver);
  260. MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
  261. MODULE_DESCRIPTION("Linear Technology LTC4306, LTC4305 I2C mux/switch driver");
  262. MODULE_LICENSE("GPL v2");