max732x.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * max732x.c - I2C Port Expander with 8/16 I/O
  3. *
  4. * Copyright (C) 2007 Marvell International Ltd.
  5. * Copyright (C) 2008 Jack Ren <jack.ren@marvell.com>
  6. * Copyright (C) 2008 Eric Miao <eric.miao@marvell.com>
  7. *
  8. * Derived from drivers/gpio/pca953x.c
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; version 2 of the License.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/slab.h>
  17. #include <linux/string.h>
  18. #include <linux/gpio.h>
  19. #include <linux/i2c.h>
  20. #include <linux/i2c/max732x.h>
  21. /*
  22. * Each port of MAX732x (including MAX7319) falls into one of the
  23. * following three types:
  24. *
  25. * - Push Pull Output
  26. * - Input
  27. * - Open Drain I/O
  28. *
  29. * designated by 'O', 'I' and 'P' individually according to MAXIM's
  30. * datasheets.
  31. *
  32. * There are two groups of I/O ports, each group usually includes
  33. * up to 8 I/O ports, and is accessed by a specific I2C address:
  34. *
  35. * - Group A : by I2C address 0b'110xxxx
  36. * - Group B : by I2C address 0b'101xxxx
  37. *
  38. * where 'xxxx' is decided by the connections of pin AD2/AD0. The
  39. * address used also affects the initial state of output signals.
  40. *
  41. * Within each group of ports, there are five known combinations of
  42. * I/O ports: 4I4O, 4P4O, 8I, 8P, 8O, see the definitions below for
  43. * the detailed organization of these ports.
  44. *
  45. * GPIO numbers start from 'gpio_base + 0' to 'gpio_base + 8/16',
  46. * and GPIOs from GROUP_A are numbered before those from GROUP_B
  47. * (if there are two groups).
  48. *
  49. * NOTE: MAX7328/MAX7329 are drop-in replacements for PCF8574/a, so
  50. * they are not supported by this driver.
  51. */
  52. #define PORT_NONE 0x0 /* '/' No Port */
  53. #define PORT_OUTPUT 0x1 /* 'O' Push-Pull, Output Only */
  54. #define PORT_INPUT 0x2 /* 'I' Input Only */
  55. #define PORT_OPENDRAIN 0x3 /* 'P' Open-Drain, I/O */
  56. #define IO_4I4O 0x5AA5 /* O7 O6 I5 I4 I3 I2 O1 O0 */
  57. #define IO_4P4O 0x5FF5 /* O7 O6 P5 P4 P3 P2 O1 O0 */
  58. #define IO_8I 0xAAAA /* I7 I6 I5 I4 I3 I2 I1 I0 */
  59. #define IO_8P 0xFFFF /* P7 P6 P5 P4 P3 P2 P1 P0 */
  60. #define IO_8O 0x5555 /* O7 O6 O5 O4 O3 O2 O1 O0 */
  61. #define GROUP_A(x) ((x) & 0xffff) /* I2C Addr: 0b'110xxxx */
  62. #define GROUP_B(x) ((x) << 16) /* I2C Addr: 0b'101xxxx */
  63. static const struct i2c_device_id max732x_id[] = {
  64. { "max7319", GROUP_A(IO_8I) },
  65. { "max7320", GROUP_B(IO_8O) },
  66. { "max7321", GROUP_A(IO_8P) },
  67. { "max7322", GROUP_A(IO_4I4O) },
  68. { "max7323", GROUP_A(IO_4P4O) },
  69. { "max7324", GROUP_A(IO_8I) | GROUP_B(IO_8O) },
  70. { "max7325", GROUP_A(IO_8P) | GROUP_B(IO_8O) },
  71. { "max7326", GROUP_A(IO_4I4O) | GROUP_B(IO_8O) },
  72. { "max7327", GROUP_A(IO_4P4O) | GROUP_B(IO_8O) },
  73. { },
  74. };
  75. MODULE_DEVICE_TABLE(i2c, max732x_id);
  76. struct max732x_chip {
  77. struct gpio_chip gpio_chip;
  78. struct i2c_client *client; /* "main" client */
  79. struct i2c_client *client_dummy;
  80. struct i2c_client *client_group_a;
  81. struct i2c_client *client_group_b;
  82. unsigned int mask_group_a;
  83. unsigned int dir_input;
  84. unsigned int dir_output;
  85. struct mutex lock;
  86. uint8_t reg_out[2];
  87. };
  88. static int max732x_write(struct max732x_chip *chip, int group_a, uint8_t val)
  89. {
  90. struct i2c_client *client;
  91. int ret;
  92. client = group_a ? chip->client_group_a : chip->client_group_b;
  93. ret = i2c_smbus_write_byte(client, val);
  94. if (ret < 0) {
  95. dev_err(&client->dev, "failed writing\n");
  96. return ret;
  97. }
  98. return 0;
  99. }
  100. static int max732x_read(struct max732x_chip *chip, int group_a, uint8_t *val)
  101. {
  102. struct i2c_client *client;
  103. int ret;
  104. client = group_a ? chip->client_group_a : chip->client_group_b;
  105. ret = i2c_smbus_read_byte(client);
  106. if (ret < 0) {
  107. dev_err(&client->dev, "failed reading\n");
  108. return ret;
  109. }
  110. *val = (uint8_t)ret;
  111. return 0;
  112. }
  113. static inline int is_group_a(struct max732x_chip *chip, unsigned off)
  114. {
  115. return (1u << off) & chip->mask_group_a;
  116. }
  117. static int max732x_gpio_get_value(struct gpio_chip *gc, unsigned off)
  118. {
  119. struct max732x_chip *chip;
  120. uint8_t reg_val;
  121. int ret;
  122. chip = container_of(gc, struct max732x_chip, gpio_chip);
  123. ret = max732x_read(chip, is_group_a(chip, off), &reg_val);
  124. if (ret < 0)
  125. return 0;
  126. return reg_val & (1u << (off & 0x7));
  127. }
  128. static void max732x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
  129. {
  130. struct max732x_chip *chip;
  131. uint8_t reg_out, mask = 1u << (off & 0x7);
  132. int ret;
  133. chip = container_of(gc, struct max732x_chip, gpio_chip);
  134. mutex_lock(&chip->lock);
  135. reg_out = (off > 7) ? chip->reg_out[1] : chip->reg_out[0];
  136. reg_out = (val) ? reg_out | mask : reg_out & ~mask;
  137. ret = max732x_write(chip, is_group_a(chip, off), reg_out);
  138. if (ret < 0)
  139. goto out;
  140. /* update the shadow register then */
  141. if (off > 7)
  142. chip->reg_out[1] = reg_out;
  143. else
  144. chip->reg_out[0] = reg_out;
  145. out:
  146. mutex_unlock(&chip->lock);
  147. }
  148. static int max732x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
  149. {
  150. struct max732x_chip *chip;
  151. unsigned int mask = 1u << off;
  152. chip = container_of(gc, struct max732x_chip, gpio_chip);
  153. if ((mask & chip->dir_input) == 0) {
  154. dev_dbg(&chip->client->dev, "%s port %d is output only\n",
  155. chip->client->name, off);
  156. return -EACCES;
  157. }
  158. return 0;
  159. }
  160. static int max732x_gpio_direction_output(struct gpio_chip *gc,
  161. unsigned off, int val)
  162. {
  163. struct max732x_chip *chip;
  164. unsigned int mask = 1u << off;
  165. chip = container_of(gc, struct max732x_chip, gpio_chip);
  166. if ((mask & chip->dir_output) == 0) {
  167. dev_dbg(&chip->client->dev, "%s port %d is input only\n",
  168. chip->client->name, off);
  169. return -EACCES;
  170. }
  171. max732x_gpio_set_value(gc, off, val);
  172. return 0;
  173. }
  174. static int __devinit max732x_setup_gpio(struct max732x_chip *chip,
  175. const struct i2c_device_id *id,
  176. unsigned gpio_start)
  177. {
  178. struct gpio_chip *gc = &chip->gpio_chip;
  179. uint32_t id_data = id->driver_data;
  180. int i, port = 0;
  181. for (i = 0; i < 16; i++, id_data >>= 2) {
  182. unsigned int mask = 1 << port;
  183. switch (id_data & 0x3) {
  184. case PORT_OUTPUT:
  185. chip->dir_output |= mask;
  186. break;
  187. case PORT_INPUT:
  188. chip->dir_input |= mask;
  189. break;
  190. case PORT_OPENDRAIN:
  191. chip->dir_output |= mask;
  192. chip->dir_input |= mask;
  193. break;
  194. default:
  195. continue;
  196. }
  197. if (i < 8)
  198. chip->mask_group_a |= mask;
  199. port++;
  200. }
  201. if (chip->dir_input)
  202. gc->direction_input = max732x_gpio_direction_input;
  203. if (chip->dir_output) {
  204. gc->direction_output = max732x_gpio_direction_output;
  205. gc->set = max732x_gpio_set_value;
  206. }
  207. gc->get = max732x_gpio_get_value;
  208. gc->can_sleep = 1;
  209. gc->base = gpio_start;
  210. gc->ngpio = port;
  211. gc->label = chip->client->name;
  212. gc->owner = THIS_MODULE;
  213. return port;
  214. }
  215. static int __devinit max732x_probe(struct i2c_client *client,
  216. const struct i2c_device_id *id)
  217. {
  218. struct max732x_platform_data *pdata;
  219. struct max732x_chip *chip;
  220. struct i2c_client *c;
  221. uint16_t addr_a, addr_b;
  222. int ret, nr_port;
  223. pdata = client->dev.platform_data;
  224. if (pdata == NULL) {
  225. dev_dbg(&client->dev, "no platform data\n");
  226. return -EINVAL;
  227. }
  228. chip = kzalloc(sizeof(struct max732x_chip), GFP_KERNEL);
  229. if (chip == NULL)
  230. return -ENOMEM;
  231. chip->client = client;
  232. nr_port = max732x_setup_gpio(chip, id, pdata->gpio_base);
  233. addr_a = (client->addr & 0x0f) | 0x60;
  234. addr_b = (client->addr & 0x0f) | 0x50;
  235. switch (client->addr & 0x70) {
  236. case 0x60:
  237. chip->client_group_a = client;
  238. if (nr_port > 7) {
  239. c = i2c_new_dummy(client->adapter, addr_b);
  240. chip->client_group_b = chip->client_dummy = c;
  241. }
  242. break;
  243. case 0x50:
  244. chip->client_group_b = client;
  245. if (nr_port > 7) {
  246. c = i2c_new_dummy(client->adapter, addr_a);
  247. chip->client_group_a = chip->client_dummy = c;
  248. }
  249. break;
  250. default:
  251. dev_err(&client->dev, "invalid I2C address specified %02x\n",
  252. client->addr);
  253. ret = -EINVAL;
  254. goto out_failed;
  255. }
  256. mutex_init(&chip->lock);
  257. max732x_read(chip, is_group_a(chip, 0), &chip->reg_out[0]);
  258. if (nr_port > 7)
  259. max732x_read(chip, is_group_a(chip, 8), &chip->reg_out[1]);
  260. ret = gpiochip_add(&chip->gpio_chip);
  261. if (ret)
  262. goto out_failed;
  263. if (pdata->setup) {
  264. ret = pdata->setup(client, chip->gpio_chip.base,
  265. chip->gpio_chip.ngpio, pdata->context);
  266. if (ret < 0)
  267. dev_warn(&client->dev, "setup failed, %d\n", ret);
  268. }
  269. i2c_set_clientdata(client, chip);
  270. return 0;
  271. out_failed:
  272. kfree(chip);
  273. return ret;
  274. }
  275. static int __devexit max732x_remove(struct i2c_client *client)
  276. {
  277. struct max732x_platform_data *pdata = client->dev.platform_data;
  278. struct max732x_chip *chip = i2c_get_clientdata(client);
  279. int ret;
  280. if (pdata->teardown) {
  281. ret = pdata->teardown(client, chip->gpio_chip.base,
  282. chip->gpio_chip.ngpio, pdata->context);
  283. if (ret < 0) {
  284. dev_err(&client->dev, "%s failed, %d\n",
  285. "teardown", ret);
  286. return ret;
  287. }
  288. }
  289. ret = gpiochip_remove(&chip->gpio_chip);
  290. if (ret) {
  291. dev_err(&client->dev, "%s failed, %d\n",
  292. "gpiochip_remove()", ret);
  293. return ret;
  294. }
  295. /* unregister any dummy i2c_client */
  296. if (chip->client_dummy)
  297. i2c_unregister_device(chip->client_dummy);
  298. kfree(chip);
  299. return 0;
  300. }
  301. static struct i2c_driver max732x_driver = {
  302. .driver = {
  303. .name = "max732x",
  304. .owner = THIS_MODULE,
  305. },
  306. .probe = max732x_probe,
  307. .remove = __devexit_p(max732x_remove),
  308. .id_table = max732x_id,
  309. };
  310. static int __init max732x_init(void)
  311. {
  312. return i2c_add_driver(&max732x_driver);
  313. }
  314. /* register after i2c postcore initcall and before
  315. * subsys initcalls that may rely on these GPIOs
  316. */
  317. subsys_initcall(max732x_init);
  318. static void __exit max732x_exit(void)
  319. {
  320. i2c_del_driver(&max732x_driver);
  321. }
  322. module_exit(max732x_exit);
  323. MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
  324. MODULE_DESCRIPTION("GPIO expander driver for MAX732X");
  325. MODULE_LICENSE("GPL");