Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 1 | /* |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 2 | * I2C driver for Marvell 88PM860x |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2009 Marvell International Ltd. |
| 5 | * Haojian Zhuang <haojian.zhuang@marvell.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/platform_device.h> |
| 14 | #include <linux/i2c.h> |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 15 | #include <linux/mfd/88pm860x.h> |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 16 | |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 17 | static inline int pm860x_read_device(struct i2c_client *i2c, |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 18 | int reg, int bytes, void *dest) |
| 19 | { |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 20 | unsigned char data; |
| 21 | int ret; |
| 22 | |
| 23 | data = (unsigned char)reg; |
| 24 | ret = i2c_master_send(i2c, &data, 1); |
| 25 | if (ret < 0) |
| 26 | return ret; |
| 27 | |
| 28 | ret = i2c_master_recv(i2c, dest, bytes); |
| 29 | if (ret < 0) |
| 30 | return ret; |
| 31 | return 0; |
| 32 | } |
| 33 | |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 34 | static inline int pm860x_write_device(struct i2c_client *i2c, |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 35 | int reg, int bytes, void *src) |
| 36 | { |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 37 | unsigned char buf[bytes + 1]; |
| 38 | int ret; |
| 39 | |
| 40 | buf[0] = (unsigned char)reg; |
| 41 | memcpy(&buf[1], src, bytes); |
| 42 | |
| 43 | ret = i2c_master_send(i2c, buf, bytes + 1); |
| 44 | if (ret < 0) |
| 45 | return ret; |
| 46 | return 0; |
| 47 | } |
| 48 | |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 49 | int pm860x_reg_read(struct i2c_client *i2c, int reg) |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 50 | { |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 51 | struct pm860x_chip *chip = i2c_get_clientdata(i2c); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 52 | unsigned char data; |
| 53 | int ret; |
| 54 | |
| 55 | mutex_lock(&chip->io_lock); |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 56 | ret = pm860x_read_device(i2c, reg, 1, &data); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 57 | mutex_unlock(&chip->io_lock); |
| 58 | |
| 59 | if (ret < 0) |
| 60 | return ret; |
| 61 | else |
| 62 | return (int)data; |
| 63 | } |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 64 | EXPORT_SYMBOL(pm860x_reg_read); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 65 | |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 66 | int pm860x_reg_write(struct i2c_client *i2c, int reg, |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 67 | unsigned char data) |
| 68 | { |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 69 | struct pm860x_chip *chip = i2c_get_clientdata(i2c); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 70 | int ret; |
| 71 | |
| 72 | mutex_lock(&chip->io_lock); |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 73 | ret = pm860x_write_device(i2c, reg, 1, &data); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 74 | mutex_unlock(&chip->io_lock); |
| 75 | |
| 76 | return ret; |
| 77 | } |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 78 | EXPORT_SYMBOL(pm860x_reg_write); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 79 | |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 80 | int pm860x_bulk_read(struct i2c_client *i2c, int reg, |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 81 | int count, unsigned char *buf) |
| 82 | { |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 83 | struct pm860x_chip *chip = i2c_get_clientdata(i2c); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 84 | int ret; |
| 85 | |
| 86 | mutex_lock(&chip->io_lock); |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 87 | ret = pm860x_read_device(i2c, reg, count, buf); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 88 | mutex_unlock(&chip->io_lock); |
| 89 | |
| 90 | return ret; |
| 91 | } |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 92 | EXPORT_SYMBOL(pm860x_bulk_read); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 93 | |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 94 | int pm860x_bulk_write(struct i2c_client *i2c, int reg, |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 95 | int count, unsigned char *buf) |
| 96 | { |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 97 | struct pm860x_chip *chip = i2c_get_clientdata(i2c); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 98 | int ret; |
| 99 | |
| 100 | mutex_lock(&chip->io_lock); |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 101 | ret = pm860x_write_device(i2c, reg, count, buf); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 102 | mutex_unlock(&chip->io_lock); |
| 103 | |
| 104 | return ret; |
| 105 | } |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 106 | EXPORT_SYMBOL(pm860x_bulk_write); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 107 | |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 108 | int pm860x_set_bits(struct i2c_client *i2c, int reg, |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 109 | unsigned char mask, unsigned char data) |
| 110 | { |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 111 | struct pm860x_chip *chip = i2c_get_clientdata(i2c); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 112 | unsigned char value; |
| 113 | int ret; |
| 114 | |
| 115 | mutex_lock(&chip->io_lock); |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 116 | ret = pm860x_read_device(i2c, reg, 1, &value); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 117 | if (ret < 0) |
| 118 | goto out; |
| 119 | value &= ~mask; |
| 120 | value |= data; |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 121 | ret = pm860x_write_device(i2c, reg, 1, &value); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 122 | out: |
| 123 | mutex_unlock(&chip->io_lock); |
| 124 | return ret; |
| 125 | } |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 126 | EXPORT_SYMBOL(pm860x_set_bits); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 127 | |
| 128 | |
| 129 | static const struct i2c_device_id pm860x_id_table[] = { |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 130 | { "88PM860x", 0 }, |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 131 | {} |
| 132 | }; |
| 133 | MODULE_DEVICE_TABLE(i2c, pm860x_id_table); |
| 134 | |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 135 | static int verify_addr(struct i2c_client *i2c) |
| 136 | { |
| 137 | unsigned short addr_8607[] = {0x30, 0x34}; |
| 138 | unsigned short addr_8606[] = {0x10, 0x11}; |
| 139 | int size, i; |
| 140 | |
| 141 | if (i2c == NULL) |
| 142 | return 0; |
| 143 | size = ARRAY_SIZE(addr_8606); |
| 144 | for (i = 0; i < size; i++) { |
| 145 | if (i2c->addr == *(addr_8606 + i)) |
| 146 | return CHIP_PM8606; |
| 147 | } |
| 148 | size = ARRAY_SIZE(addr_8607); |
| 149 | for (i = 0; i < size; i++) { |
| 150 | if (i2c->addr == *(addr_8607 + i)) |
| 151 | return CHIP_PM8607; |
| 152 | } |
| 153 | return 0; |
| 154 | } |
| 155 | |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 156 | static int __devinit pm860x_probe(struct i2c_client *client, |
| 157 | const struct i2c_device_id *id) |
| 158 | { |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 159 | struct pm860x_platform_data *pdata = client->dev.platform_data; |
| 160 | static struct pm860x_chip *chip; |
| 161 | struct i2c_board_info i2c_info = { |
| 162 | .type = "88PM860x", |
| 163 | .platform_data = client->dev.platform_data, |
| 164 | }; |
| 165 | int addr_c, found_companion = 0; |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 166 | |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 167 | if (pdata == NULL) { |
| 168 | pr_info("No platform data in %s!\n", __func__); |
| 169 | return -EINVAL; |
| 170 | } |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 171 | |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 172 | /* |
| 173 | * Both client and companion client shares same platform driver. |
| 174 | * Driver distinguishes them by pdata->companion_addr. |
| 175 | * pdata->companion_addr is only assigned if companion chip exists. |
| 176 | * At the same time, the companion_addr shouldn't equal to client |
| 177 | * address. |
| 178 | */ |
| 179 | addr_c = pdata->companion_addr; |
| 180 | if (addr_c && (addr_c != client->addr)) { |
| 181 | i2c_info.addr = addr_c; |
| 182 | found_companion = 1; |
| 183 | } |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 184 | |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 185 | if (found_companion || (addr_c == 0)) { |
| 186 | chip = kzalloc(sizeof(struct pm860x_chip), GFP_KERNEL); |
| 187 | if (chip == NULL) |
| 188 | return -ENOMEM; |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 189 | |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 190 | chip->id = verify_addr(client); |
| 191 | chip->companion_addr = addr_c; |
| 192 | chip->client = client; |
| 193 | i2c_set_clientdata(client, chip); |
| 194 | chip->dev = &client->dev; |
| 195 | mutex_init(&chip->io_lock); |
| 196 | dev_set_drvdata(chip->dev, chip); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 197 | |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 198 | if (found_companion) { |
| 199 | /* |
| 200 | * If this driver is built in, probe function is |
| 201 | * recursive. |
| 202 | * If this driver is built as module, the next probe |
| 203 | * function is called after the first one finished. |
| 204 | */ |
| 205 | chip->companion = i2c_new_device(client->adapter, |
| 206 | &i2c_info); |
| 207 | } |
| 208 | } |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 209 | |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 210 | /* |
| 211 | * If companion chip existes, it's called by companion probe. |
| 212 | * If there's no companion chip, it's called by client probe. |
| 213 | */ |
| 214 | if ((addr_c == 0) || (addr_c == client->addr)) { |
| 215 | chip->companion = client; |
| 216 | i2c_set_clientdata(chip->companion, chip); |
| 217 | pm860x_device_init(chip, pdata); |
| 218 | } |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 219 | return 0; |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static int __devexit pm860x_remove(struct i2c_client *client) |
| 223 | { |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 224 | struct pm860x_chip *chip = i2c_get_clientdata(client); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 225 | |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 226 | /* |
| 227 | * If companion existes, companion client is removed first. |
| 228 | * Because companion client is registered last and removed first. |
| 229 | */ |
| 230 | if (chip->companion_addr == client->addr) |
| 231 | return 0; |
| 232 | pm860x_device_exit(chip); |
| 233 | i2c_unregister_device(chip->companion); |
| 234 | i2c_set_clientdata(chip->companion, NULL); |
| 235 | i2c_set_clientdata(chip->client, NULL); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 236 | kfree(chip); |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | static struct i2c_driver pm860x_driver = { |
| 241 | .driver = { |
| 242 | .name = "88PM860x", |
| 243 | .owner = THIS_MODULE, |
| 244 | }, |
| 245 | .probe = pm860x_probe, |
| 246 | .remove = __devexit_p(pm860x_remove), |
| 247 | .id_table = pm860x_id_table, |
| 248 | }; |
| 249 | |
| 250 | static int __init pm860x_i2c_init(void) |
| 251 | { |
| 252 | int ret; |
| 253 | ret = i2c_add_driver(&pm860x_driver); |
| 254 | if (ret != 0) |
| 255 | pr_err("Failed to register 88PM860x I2C driver: %d\n", ret); |
| 256 | return ret; |
| 257 | } |
| 258 | subsys_initcall(pm860x_i2c_init); |
| 259 | |
| 260 | static void __exit pm860x_i2c_exit(void) |
| 261 | { |
| 262 | i2c_del_driver(&pm860x_driver); |
| 263 | } |
| 264 | module_exit(pm860x_i2c_exit); |
| 265 | |
| 266 | MODULE_DESCRIPTION("I2C Driver for Marvell 88PM860x"); |
| 267 | MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>"); |
| 268 | MODULE_LICENSE("GPL"); |