Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 2 | /* |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 3 | * I2C driver for Marvell 88PM860x |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2009 Marvell International Ltd. |
Lee Jones | 0363be8 | 2014-05-08 12:55:35 +0100 | [diff] [blame] | 6 | * |
| 7 | * Author: Haojian Zhuang <haojian.zhuang@marvell.com> |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 8 | */ |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/module.h> |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 11 | #include <linux/i2c.h> |
Jett.Zhou | b46a36c | 2011-11-11 15:38:27 +0800 | [diff] [blame] | 12 | #include <linux/regmap.h> |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 13 | #include <linux/mfd/88pm860x.h> |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 14 | |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 15 | int pm860x_reg_read(struct i2c_client *i2c, int reg) |
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 | struct pm860x_chip *chip = i2c_get_clientdata(i2c); |
Jett.Zhou | b46a36c | 2011-11-11 15:38:27 +0800 | [diff] [blame] | 18 | struct regmap *map = (i2c == chip->client) ? chip->regmap |
| 19 | : chip->regmap_companion; |
| 20 | unsigned int data; |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 21 | int ret; |
| 22 | |
Jett.Zhou | b46a36c | 2011-11-11 15:38:27 +0800 | [diff] [blame] | 23 | ret = regmap_read(map, reg, &data); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 24 | if (ret < 0) |
| 25 | return ret; |
| 26 | else |
| 27 | return (int)data; |
| 28 | } |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 29 | EXPORT_SYMBOL(pm860x_reg_read); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 30 | |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 31 | int pm860x_reg_write(struct i2c_client *i2c, int reg, |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 32 | unsigned char data) |
| 33 | { |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 34 | struct pm860x_chip *chip = i2c_get_clientdata(i2c); |
Jett.Zhou | b46a36c | 2011-11-11 15:38:27 +0800 | [diff] [blame] | 35 | struct regmap *map = (i2c == chip->client) ? chip->regmap |
| 36 | : chip->regmap_companion; |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 37 | int ret; |
| 38 | |
Jett.Zhou | b46a36c | 2011-11-11 15:38:27 +0800 | [diff] [blame] | 39 | ret = regmap_write(map, reg, data); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 40 | return ret; |
| 41 | } |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 42 | EXPORT_SYMBOL(pm860x_reg_write); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 43 | |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 44 | int pm860x_bulk_read(struct i2c_client *i2c, int reg, |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 45 | int count, unsigned char *buf) |
| 46 | { |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 47 | struct pm860x_chip *chip = i2c_get_clientdata(i2c); |
Jett.Zhou | b46a36c | 2011-11-11 15:38:27 +0800 | [diff] [blame] | 48 | struct regmap *map = (i2c == chip->client) ? chip->regmap |
| 49 | : chip->regmap_companion; |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 50 | int ret; |
| 51 | |
Jett.Zhou | b46a36c | 2011-11-11 15:38:27 +0800 | [diff] [blame] | 52 | ret = regmap_raw_read(map, reg, buf, count); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 53 | return ret; |
| 54 | } |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 55 | EXPORT_SYMBOL(pm860x_bulk_read); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 56 | |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 57 | int pm860x_bulk_write(struct i2c_client *i2c, int reg, |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 58 | int count, unsigned char *buf) |
| 59 | { |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 60 | struct pm860x_chip *chip = i2c_get_clientdata(i2c); |
Jett.Zhou | b46a36c | 2011-11-11 15:38:27 +0800 | [diff] [blame] | 61 | struct regmap *map = (i2c == chip->client) ? chip->regmap |
| 62 | : chip->regmap_companion; |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 63 | int ret; |
| 64 | |
Jett.Zhou | b46a36c | 2011-11-11 15:38:27 +0800 | [diff] [blame] | 65 | ret = regmap_raw_write(map, reg, buf, count); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 66 | return ret; |
| 67 | } |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 68 | EXPORT_SYMBOL(pm860x_bulk_write); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 69 | |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 70 | int pm860x_set_bits(struct i2c_client *i2c, int reg, |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 71 | unsigned char mask, unsigned char data) |
| 72 | { |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 73 | struct pm860x_chip *chip = i2c_get_clientdata(i2c); |
Jett.Zhou | b46a36c | 2011-11-11 15:38:27 +0800 | [diff] [blame] | 74 | struct regmap *map = (i2c == chip->client) ? chip->regmap |
| 75 | : chip->regmap_companion; |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 76 | int ret; |
| 77 | |
Jett.Zhou | b46a36c | 2011-11-11 15:38:27 +0800 | [diff] [blame] | 78 | ret = regmap_update_bits(map, reg, mask, data); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 79 | return ret; |
| 80 | } |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 81 | EXPORT_SYMBOL(pm860x_set_bits); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 82 | |
Jett.Zhou | 5bdf741 | 2011-11-11 15:38:26 +0800 | [diff] [blame] | 83 | static int read_device(struct i2c_client *i2c, int reg, |
| 84 | int bytes, void *dest) |
| 85 | { |
| 86 | unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX + 3]; |
| 87 | unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX + 2]; |
| 88 | struct i2c_adapter *adap = i2c->adapter; |
Shubhrajyoti D | 4894222 | 2012-09-17 21:14:58 +0530 | [diff] [blame] | 89 | struct i2c_msg msg[2] = { |
| 90 | { |
| 91 | .addr = i2c->addr, |
| 92 | .flags = 0, |
| 93 | .len = 1, |
| 94 | .buf = msgbuf0 |
| 95 | }, |
| 96 | { .addr = i2c->addr, |
| 97 | .flags = I2C_M_RD, |
| 98 | .len = 0, |
| 99 | .buf = msgbuf1 |
| 100 | }, |
Jett.Zhou | 5bdf741 | 2011-11-11 15:38:26 +0800 | [diff] [blame] | 101 | }; |
| 102 | int num = 1, ret = 0; |
| 103 | |
| 104 | if (dest == NULL) |
| 105 | return -EINVAL; |
| 106 | msgbuf0[0] = (unsigned char)reg; /* command */ |
| 107 | msg[1].len = bytes; |
| 108 | |
| 109 | /* if data needs to read back, num should be 2 */ |
| 110 | if (bytes > 0) |
| 111 | num = 2; |
| 112 | ret = adap->algo->master_xfer(adap, msg, num); |
| 113 | memcpy(dest, msgbuf1, bytes); |
| 114 | if (ret < 0) |
| 115 | return ret; |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static int write_device(struct i2c_client *i2c, int reg, |
| 120 | int bytes, void *src) |
| 121 | { |
Lee Jones | 7a36ceb | 2014-08-28 10:51:26 +0100 | [diff] [blame] | 122 | unsigned char buf[2]; |
Jett.Zhou | 5bdf741 | 2011-11-11 15:38:26 +0800 | [diff] [blame] | 123 | struct i2c_adapter *adap = i2c->adapter; |
| 124 | struct i2c_msg msg; |
| 125 | int ret; |
| 126 | |
| 127 | buf[0] = (unsigned char)reg; |
| 128 | memcpy(&buf[1], src, bytes); |
| 129 | msg.addr = i2c->addr; |
| 130 | msg.flags = 0; |
| 131 | msg.len = bytes + 1; |
| 132 | msg.buf = buf; |
| 133 | |
| 134 | ret = adap->algo->master_xfer(adap, &msg, 1); |
| 135 | if (ret < 0) |
| 136 | return ret; |
| 137 | return 0; |
| 138 | } |
| 139 | |
Haojian Zhuang | 09b0341 | 2011-03-07 23:43:16 +0800 | [diff] [blame] | 140 | int pm860x_page_reg_write(struct i2c_client *i2c, int reg, |
| 141 | unsigned char data) |
| 142 | { |
Haojian Zhuang | 09b0341 | 2011-03-07 23:43:16 +0800 | [diff] [blame] | 143 | unsigned char zero; |
| 144 | int ret; |
| 145 | |
Peter Rosin | 8c8f74f | 2018-06-20 07:18:02 +0200 | [diff] [blame] | 146 | i2c_lock_bus(i2c->adapter, I2C_LOCK_SEGMENT); |
Jett.Zhou | 5bdf741 | 2011-11-11 15:38:26 +0800 | [diff] [blame] | 147 | read_device(i2c, 0xFA, 0, &zero); |
| 148 | read_device(i2c, 0xFB, 0, &zero); |
| 149 | read_device(i2c, 0xFF, 0, &zero); |
| 150 | ret = write_device(i2c, reg, 1, &data); |
| 151 | read_device(i2c, 0xFE, 0, &zero); |
| 152 | read_device(i2c, 0xFC, 0, &zero); |
Peter Rosin | 8c8f74f | 2018-06-20 07:18:02 +0200 | [diff] [blame] | 153 | i2c_unlock_bus(i2c->adapter, I2C_LOCK_SEGMENT); |
Haojian Zhuang | 09b0341 | 2011-03-07 23:43:16 +0800 | [diff] [blame] | 154 | return ret; |
| 155 | } |
| 156 | EXPORT_SYMBOL(pm860x_page_reg_write); |
| 157 | |
| 158 | int pm860x_page_bulk_read(struct i2c_client *i2c, int reg, |
| 159 | int count, unsigned char *buf) |
| 160 | { |
Haojian Zhuang | 09b0341 | 2011-03-07 23:43:16 +0800 | [diff] [blame] | 161 | unsigned char zero = 0; |
| 162 | int ret; |
| 163 | |
Peter Rosin | 8c8f74f | 2018-06-20 07:18:02 +0200 | [diff] [blame] | 164 | i2c_lock_bus(i2c->adapter, I2C_LOCK_SEGMENT); |
Jett.Zhou | 5bdf741 | 2011-11-11 15:38:26 +0800 | [diff] [blame] | 165 | read_device(i2c, 0xfa, 0, &zero); |
| 166 | read_device(i2c, 0xfb, 0, &zero); |
| 167 | read_device(i2c, 0xff, 0, &zero); |
| 168 | ret = read_device(i2c, reg, count, buf); |
| 169 | read_device(i2c, 0xFE, 0, &zero); |
| 170 | read_device(i2c, 0xFC, 0, &zero); |
Peter Rosin | 8c8f74f | 2018-06-20 07:18:02 +0200 | [diff] [blame] | 171 | i2c_unlock_bus(i2c->adapter, I2C_LOCK_SEGMENT); |
Haojian Zhuang | 09b0341 | 2011-03-07 23:43:16 +0800 | [diff] [blame] | 172 | return ret; |
| 173 | } |
| 174 | EXPORT_SYMBOL(pm860x_page_bulk_read); |