lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * zx234290-i2c.c -- I2C access for ZTE ZX234290 PMIC |
| 3 | * |
| 4 | * Copyright 2016 ZTE Inc. |
| 5 | * |
| 6 | * Author: yuxiang<yu.xiang5@zte.com.cn> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the |
| 10 | * Free Software Foundation; either version 2 of the License, or (at your |
| 11 | * option) any later version. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/moduleparam.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/gpio.h> |
| 20 | #include <linux/i2c.h> |
| 21 | #include <linux/mfd/core.h> |
| 22 | #include <linux/mfd/zx234290.h> |
| 23 | #include <linux/wakelock.h> |
| 24 | |
| 25 | static struct i2c_client *zx234290_i2c =0; |
| 26 | static DEFINE_MUTEX(zx234290_i2c_lock); |
| 27 | |
| 28 | int zx29_i2c_xfer_PSM(struct i2c_adapter *adap, struct i2c_msg *msgs, int num); |
| 29 | #if 1 |
| 30 | int zx234290_i2c_read_simple(u8 reg, void *dest) |
| 31 | { |
| 32 | mutex_lock(&zx234290_i2c_lock); |
| 33 | // struct i2c_client *i2c = zx234290->control_data; |
| 34 | struct i2c_client *i2c = zx234290_i2c; |
| 35 | struct i2c_msg xfer[2]; |
| 36 | int ret; |
| 37 | |
| 38 | /* Write register */ |
| 39 | xfer[0].addr = i2c->addr; |
| 40 | xfer[0].flags = 0; |
| 41 | xfer[0].len = 1; |
| 42 | xfer[0].buf = ® |
| 43 | |
| 44 | /* Read data */ |
| 45 | xfer[1].addr = i2c->addr; |
| 46 | xfer[1].flags = I2C_M_RD; |
| 47 | xfer[1].len = 1; |
| 48 | xfer[1].buf = dest; |
| 49 | |
| 50 | ret = i2c_transfer(i2c->adapter, xfer, 2); |
| 51 | if (ret == 2) |
| 52 | ret = 0; |
| 53 | else if (ret >= 0) |
| 54 | { |
| 55 | ret = -EIO; |
| 56 | |
| 57 | printk(KERN_INFO "[zx234290 i2c]read-1 error. reg:%x\n", reg); |
| 58 | } |
| 59 | mutex_unlock(&zx234290_i2c_lock); |
| 60 | |
| 61 | return ret; |
| 62 | } |
| 63 | EXPORT_SYMBOL_GPL(zx234290_i2c_read_simple); |
| 64 | |
| 65 | int zx234290_i2c_read_simple_PSM(u8 reg, void *dest) |
| 66 | { |
| 67 | // struct i2c_client *i2c = zx234290->control_data; |
| 68 | struct i2c_client *i2c = zx234290_i2c; |
| 69 | struct i2c_msg xfer[2]; |
| 70 | int ret; |
| 71 | |
| 72 | /* Write register */ |
| 73 | xfer[0].addr = i2c->addr; |
| 74 | xfer[0].flags = 0; |
| 75 | xfer[0].len = 1; |
| 76 | xfer[0].buf = ® |
| 77 | |
| 78 | /* Read data */ |
| 79 | xfer[1].addr = i2c->addr; |
| 80 | xfer[1].flags = I2C_M_RD; |
| 81 | xfer[1].len = 1; |
| 82 | xfer[1].buf = dest; |
| 83 | |
| 84 | ret = zx29_i2c_xfer_PSM(i2c->adapter, xfer, 2); |
| 85 | if (ret == 2) |
| 86 | ret = 0; |
| 87 | else |
| 88 | { |
| 89 | ret = -EIO; |
| 90 | |
| 91 | printk(KERN_INFO "[zx234290 i2c]read-psm error. reg:%x\n", reg); |
| 92 | } |
| 93 | |
| 94 | return ret; |
| 95 | } |
| 96 | EXPORT_SYMBOL_GPL(zx234290_i2c_read_simple_PSM); |
| 97 | |
| 98 | int zx234290_i2c_write_simple(u8 reg, void *src) |
| 99 | { |
| 100 | mutex_lock(&zx234290_i2c_lock); |
| 101 | // struct i2c_client *i2c = zx234290->control_data; |
| 102 | struct i2c_client *i2c = zx234290_i2c; |
| 103 | /* we add 1 byte for device register */ |
| 104 | u8 msg[4]; |
| 105 | int ret; |
| 106 | |
| 107 | //if (reg > ZX234290_MAX_REGISTER) |
| 108 | // return -EINVAL; |
| 109 | |
| 110 | msg[0] = reg; |
| 111 | memcpy(&msg[1], src, 1); |
| 112 | |
| 113 | ret = i2c_master_send(i2c, msg, 1 + 1); |
| 114 | if (ret < 0){ |
| 115 | mutex_unlock(&zx234290_i2c_lock); |
| 116 | |
| 117 | printk(KERN_INFO "[zx234290 i2c]write-3 error. reg:%x\n", reg); |
| 118 | |
| 119 | return ret; |
| 120 | } |
| 121 | if (ret != 1 + 1){ |
| 122 | mutex_unlock(&zx234290_i2c_lock); |
| 123 | printk(KERN_INFO "[zx234290 i2c]write-4 error. reg:%x\n", reg); |
| 124 | return -EIO; |
| 125 | } |
| 126 | |
| 127 | mutex_unlock(&zx234290_i2c_lock); |
| 128 | return 0; |
| 129 | } |
| 130 | EXPORT_SYMBOL_GPL(zx234290_i2c_write_simple); |
| 131 | |
| 132 | int zx234290_i2c_write_simple_PSM(u8 reg, void *src) |
| 133 | { |
| 134 | // struct i2c_client *i2c = zx234290->control_data; |
| 135 | struct i2c_client *i2c = zx234290_i2c; |
| 136 | /* we add 1 byte for device register */ |
| 137 | u8 buf[4]; |
| 138 | int ret; |
| 139 | struct i2c_msg msg; |
| 140 | |
| 141 | if (reg > ZX234290_MAX_REGISTER) |
| 142 | return -EINVAL; |
| 143 | |
| 144 | buf[0] = reg; |
| 145 | memcpy(buf+1, src, 1); |
| 146 | |
| 147 | msg.addr = i2c->addr; |
| 148 | msg.flags = 0; |
| 149 | msg.len = 2; |
| 150 | msg.buf = (char *)buf; |
| 151 | |
| 152 | ret = zx29_i2c_xfer_PSM(i2c->adapter, &msg, 1); |
| 153 | if (ret != 1) { |
| 154 | printk(KERN_INFO "[zx234290 i2c]write-psm error. reg:%x\n", reg); |
| 155 | |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | EXPORT_SYMBOL_GPL(zx234290_i2c_write_simple_PSM); |
| 162 | #endif |
| 163 | |
| 164 | |
| 165 | |
| 166 | //static i2c_client *zx234290_i2c=0; |
| 167 | #if 1 |
| 168 | static int zx234290_i2c_read(struct zx234290 *zx234290, u8 reg, |
| 169 | int bytes, void *dest) |
| 170 | { |
| 171 | struct i2c_client *i2c = zx234290->control_data; |
| 172 | // struct i2c_client *i2c = zx234290_i2c; |
| 173 | struct i2c_msg xfer[2]; |
| 174 | int ret; |
| 175 | |
| 176 | /* Write register */ |
| 177 | xfer[0].addr = i2c->addr; |
| 178 | xfer[0].flags = 0; |
| 179 | xfer[0].len = 1; |
| 180 | xfer[0].buf = ® |
| 181 | |
| 182 | /* Read data */ |
| 183 | xfer[1].addr = i2c->addr; |
| 184 | xfer[1].flags = I2C_M_RD; |
| 185 | xfer[1].len = bytes; |
| 186 | xfer[1].buf = dest; |
| 187 | |
| 188 | ret = i2c_transfer(i2c->adapter, xfer, 2); |
| 189 | if (ret == 2) |
| 190 | ret = 0; |
| 191 | else if (ret >= 0) |
| 192 | { |
| 193 | ret = -EIO; |
| 194 | |
| 195 | printk(KERN_INFO "[zx234290 i2c]read error. reg:%x\n", reg); |
| 196 | } |
| 197 | |
| 198 | return ret; |
| 199 | } |
| 200 | |
| 201 | static int zx234290_i2c_write(struct zx234290 *zx234290, u8 reg, |
| 202 | int bytes, void *src) |
| 203 | { |
| 204 | struct i2c_client *i2c = zx234290->control_data; |
| 205 | // struct i2c_client *i2c = zx234290_i2c; |
| 206 | /* we add 1 byte for device register */ |
| 207 | u8 msg[ZX234290_MAX_REGISTER + 1]; |
| 208 | int ret; |
| 209 | |
| 210 | if (bytes > ZX234290_MAX_REGISTER) |
| 211 | return -EINVAL; |
| 212 | |
| 213 | msg[0] = reg; |
| 214 | memcpy(&msg[1], src, bytes); |
| 215 | |
| 216 | ret = i2c_master_send(i2c, msg, bytes + 1); |
| 217 | if (ret < 0) |
| 218 | { |
| 219 | printk(KERN_INFO "[zx234290 i2c]write-1 error. reg:%x\n", reg); |
| 220 | |
| 221 | return ret; |
| 222 | } |
| 223 | if (ret != bytes + 1) |
| 224 | { |
| 225 | printk(KERN_INFO "[zx234290 i2c]write-2 error. reg:%x\n", reg); |
| 226 | |
| 227 | return -EIO; |
| 228 | } |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | #endif |
| 233 | |
| 234 | static int zx234290_i2c_probe(struct i2c_client *i2c, |
| 235 | const struct i2c_device_id *id) |
| 236 | { |
| 237 | struct zx234290 *zx234290; |
| 238 | |
| 239 | zx234290_i2c = i2c; |
| 240 | |
| 241 | zx234290 = kzalloc(sizeof(struct zx234290), GFP_KERNEL); |
| 242 | if (zx234290 == NULL) |
| 243 | return -ENOMEM; |
| 244 | |
| 245 | i2c_set_clientdata(i2c, zx234290); |
| 246 | zx234290->dev = &i2c->dev; |
| 247 | zx234290->control_data = i2c; |
| 248 | zx234290->read = zx234290_i2c_read; |
| 249 | zx234290->write = zx234290_i2c_write; |
| 250 | |
| 251 | return zx234290_device_init(zx234290); |
| 252 | } |
| 253 | |
| 254 | static int zx234290_i2c_remove(struct i2c_client *i2c) |
| 255 | { |
| 256 | struct zx234290 *zx234290 = i2c_get_clientdata(i2c); |
| 257 | |
| 258 | if(!zx234290) |
| 259 | return -EINVAL; |
| 260 | zx234290_device_exit(zx234290); |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | static const struct i2c_device_id zx234290_i2c_id[] = { |
| 266 | {"zx234290", 0 }, |
| 267 | { } |
| 268 | }; |
| 269 | MODULE_DEVICE_TABLE(i2c, zx234290_i2c_id); |
| 270 | |
| 271 | #if 1 |
| 272 | static struct i2c_driver zx234290_i2c_driver = { |
| 273 | .driver = { |
| 274 | .name = "zx234290", |
| 275 | .owner = THIS_MODULE, |
| 276 | }, |
| 277 | .probe = zx234290_i2c_probe, |
| 278 | .remove = zx234290_i2c_remove, |
| 279 | .id_table = zx234290_i2c_id, |
| 280 | }; |
| 281 | #endif |
| 282 | struct wake_lock adc_wake_lock; |
| 283 | static int __init zx234290_i2c_init(void) |
| 284 | { |
| 285 | int ret; |
| 286 | wake_lock_init(&adc_wake_lock, WAKE_LOCK_SUSPEND, "adc_lock"); |
| 287 | ret = i2c_add_driver(&zx234290_i2c_driver); |
| 288 | if (ret != 0) |
| 289 | pr_err("Failed to register ZX234290 I2C driver: %d\n", ret); |
| 290 | |
| 291 | return ret; |
| 292 | } |
| 293 | /* init early so consumer devices can complete system boot */ |
| 294 | subsys_initcall(zx234290_i2c_init); |
| 295 | |
| 296 | static void __exit zx234290_i2c_exit(void) |
| 297 | { |
| 298 | i2c_del_driver(&zx234290_i2c_driver); |
| 299 | } |
| 300 | module_exit(zx234290_i2c_exit); |
| 301 | |
| 302 | MODULE_AUTHOR("Dongjian"); |
| 303 | MODULE_DESCRIPTION("ZX234290 chip family multi-function driver"); |
| 304 | MODULE_LICENSE("GPL"); |