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