Greg Kroah-Hartman | 37613fa | 2019-04-25 20:06:18 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | // |
| 3 | // Register map access API - SPMI support |
| 4 | // |
| 5 | // Copyright (c) 2012-2013, The Linux Foundation. All rights reserved. |
| 6 | // |
| 7 | // Based on regmap-i2c.c: |
| 8 | // Copyright 2011 Wolfson Microelectronics plc |
| 9 | // Author: Mark Brown <broonie@opensource.wolfsonmicro.com> |
| 10 | |
Josh Cartwright | a01779f | 2013-10-28 13:12:35 -0500 | [diff] [blame] | 11 | #include <linux/regmap.h> |
| 12 | #include <linux/spmi.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/init.h> |
| 15 | |
Josh Cartwright | c9afbb0 | 2014-02-12 13:44:27 -0600 | [diff] [blame] | 16 | static int regmap_spmi_base_read(void *context, |
| 17 | const void *reg, size_t reg_size, |
| 18 | void *val, size_t val_size) |
Josh Cartwright | a01779f | 2013-10-28 13:12:35 -0500 | [diff] [blame] | 19 | { |
Josh Cartwright | c9afbb0 | 2014-02-12 13:44:27 -0600 | [diff] [blame] | 20 | u8 addr = *(u8 *)reg; |
| 21 | int err = 0; |
| 22 | |
| 23 | BUG_ON(reg_size != 1); |
| 24 | |
| 25 | while (val_size-- && !err) |
| 26 | err = spmi_register_read(context, addr++, val++); |
| 27 | |
| 28 | return err; |
Josh Cartwright | a01779f | 2013-10-28 13:12:35 -0500 | [diff] [blame] | 29 | } |
| 30 | |
Josh Cartwright | c9afbb0 | 2014-02-12 13:44:27 -0600 | [diff] [blame] | 31 | static int regmap_spmi_base_gather_write(void *context, |
| 32 | const void *reg, size_t reg_size, |
| 33 | const void *val, size_t val_size) |
Josh Cartwright | a01779f | 2013-10-28 13:12:35 -0500 | [diff] [blame] | 34 | { |
Josh Cartwright | c9afbb0 | 2014-02-12 13:44:27 -0600 | [diff] [blame] | 35 | const u8 *data = val; |
| 36 | u8 addr = *(u8 *)reg; |
| 37 | int err = 0; |
| 38 | |
| 39 | BUG_ON(reg_size != 1); |
| 40 | |
| 41 | /* |
| 42 | * SPMI defines a more bandwidth-efficient 'Register 0 Write' sequence, |
| 43 | * use it when possible. |
| 44 | */ |
| 45 | if (addr == 0 && val_size) { |
| 46 | err = spmi_register_zero_write(context, *data); |
| 47 | if (err) |
| 48 | goto err_out; |
| 49 | |
| 50 | data++; |
| 51 | addr++; |
| 52 | val_size--; |
| 53 | } |
| 54 | |
| 55 | while (val_size) { |
| 56 | err = spmi_register_write(context, addr, *data); |
| 57 | if (err) |
| 58 | goto err_out; |
| 59 | |
| 60 | data++; |
| 61 | addr++; |
| 62 | val_size--; |
| 63 | } |
| 64 | |
| 65 | err_out: |
| 66 | return err; |
Josh Cartwright | a01779f | 2013-10-28 13:12:35 -0500 | [diff] [blame] | 67 | } |
| 68 | |
Josh Cartwright | c9afbb0 | 2014-02-12 13:44:27 -0600 | [diff] [blame] | 69 | static int regmap_spmi_base_write(void *context, const void *data, |
| 70 | size_t count) |
Josh Cartwright | a01779f | 2013-10-28 13:12:35 -0500 | [diff] [blame] | 71 | { |
Josh Cartwright | c9afbb0 | 2014-02-12 13:44:27 -0600 | [diff] [blame] | 72 | BUG_ON(count < 1); |
| 73 | return regmap_spmi_base_gather_write(context, data, 1, data + 1, |
| 74 | count - 1); |
Josh Cartwright | a01779f | 2013-10-28 13:12:35 -0500 | [diff] [blame] | 75 | } |
| 76 | |
Julia Lawall | 9c2e5cb | 2017-08-11 08:57:36 +0200 | [diff] [blame] | 77 | static const struct regmap_bus regmap_spmi_base = { |
Josh Cartwright | c9afbb0 | 2014-02-12 13:44:27 -0600 | [diff] [blame] | 78 | .read = regmap_spmi_base_read, |
| 79 | .write = regmap_spmi_base_write, |
| 80 | .gather_write = regmap_spmi_base_gather_write, |
Josh Cartwright | a01779f | 2013-10-28 13:12:35 -0500 | [diff] [blame] | 81 | .reg_format_endian_default = REGMAP_ENDIAN_NATIVE, |
| 82 | .val_format_endian_default = REGMAP_ENDIAN_NATIVE, |
| 83 | }; |
| 84 | |
Nicolas Boichat | 3cfe7a7 | 2015-07-08 14:30:18 +0800 | [diff] [blame] | 85 | struct regmap *__regmap_init_spmi_base(struct spmi_device *sdev, |
| 86 | const struct regmap_config *config, |
| 87 | struct lock_class_key *lock_key, |
| 88 | const char *lock_name) |
Josh Cartwright | a01779f | 2013-10-28 13:12:35 -0500 | [diff] [blame] | 89 | { |
Nicolas Boichat | 3cfe7a7 | 2015-07-08 14:30:18 +0800 | [diff] [blame] | 90 | return __regmap_init(&sdev->dev, ®map_spmi_base, sdev, config, |
| 91 | lock_key, lock_name); |
Josh Cartwright | a01779f | 2013-10-28 13:12:35 -0500 | [diff] [blame] | 92 | } |
Nicolas Boichat | 3cfe7a7 | 2015-07-08 14:30:18 +0800 | [diff] [blame] | 93 | EXPORT_SYMBOL_GPL(__regmap_init_spmi_base); |
Josh Cartwright | a01779f | 2013-10-28 13:12:35 -0500 | [diff] [blame] | 94 | |
Nicolas Boichat | 3cfe7a7 | 2015-07-08 14:30:18 +0800 | [diff] [blame] | 95 | struct regmap *__devm_regmap_init_spmi_base(struct spmi_device *sdev, |
| 96 | const struct regmap_config *config, |
| 97 | struct lock_class_key *lock_key, |
| 98 | const char *lock_name) |
Josh Cartwright | c9afbb0 | 2014-02-12 13:44:27 -0600 | [diff] [blame] | 99 | { |
Nicolas Boichat | 3cfe7a7 | 2015-07-08 14:30:18 +0800 | [diff] [blame] | 100 | return __devm_regmap_init(&sdev->dev, ®map_spmi_base, sdev, config, |
| 101 | lock_key, lock_name); |
Josh Cartwright | c9afbb0 | 2014-02-12 13:44:27 -0600 | [diff] [blame] | 102 | } |
Nicolas Boichat | 3cfe7a7 | 2015-07-08 14:30:18 +0800 | [diff] [blame] | 103 | EXPORT_SYMBOL_GPL(__devm_regmap_init_spmi_base); |
Josh Cartwright | c9afbb0 | 2014-02-12 13:44:27 -0600 | [diff] [blame] | 104 | |
| 105 | static int regmap_spmi_ext_read(void *context, |
| 106 | const void *reg, size_t reg_size, |
| 107 | void *val, size_t val_size) |
| 108 | { |
| 109 | int err = 0; |
| 110 | size_t len; |
| 111 | u16 addr; |
| 112 | |
| 113 | BUG_ON(reg_size != 2); |
| 114 | |
| 115 | addr = *(u16 *)reg; |
| 116 | |
| 117 | /* |
| 118 | * Split accesses into two to take advantage of the more |
| 119 | * bandwidth-efficient 'Extended Register Read' command when possible |
| 120 | */ |
| 121 | while (addr <= 0xFF && val_size) { |
| 122 | len = min_t(size_t, val_size, 16); |
| 123 | |
| 124 | err = spmi_ext_register_read(context, addr, val, len); |
| 125 | if (err) |
| 126 | goto err_out; |
| 127 | |
| 128 | addr += len; |
| 129 | val += len; |
| 130 | val_size -= len; |
| 131 | } |
| 132 | |
| 133 | while (val_size) { |
| 134 | len = min_t(size_t, val_size, 8); |
| 135 | |
Jack Pham | dec8e8f | 2016-04-14 23:37:26 -0700 | [diff] [blame] | 136 | err = spmi_ext_register_readl(context, addr, val, len); |
Josh Cartwright | c9afbb0 | 2014-02-12 13:44:27 -0600 | [diff] [blame] | 137 | if (err) |
| 138 | goto err_out; |
| 139 | |
| 140 | addr += len; |
| 141 | val += len; |
| 142 | val_size -= len; |
| 143 | } |
| 144 | |
| 145 | err_out: |
| 146 | return err; |
| 147 | } |
| 148 | |
| 149 | static int regmap_spmi_ext_gather_write(void *context, |
| 150 | const void *reg, size_t reg_size, |
| 151 | const void *val, size_t val_size) |
| 152 | { |
| 153 | int err = 0; |
| 154 | size_t len; |
| 155 | u16 addr; |
| 156 | |
| 157 | BUG_ON(reg_size != 2); |
| 158 | |
| 159 | addr = *(u16 *)reg; |
| 160 | |
| 161 | while (addr <= 0xFF && val_size) { |
| 162 | len = min_t(size_t, val_size, 16); |
| 163 | |
| 164 | err = spmi_ext_register_write(context, addr, val, len); |
| 165 | if (err) |
| 166 | goto err_out; |
| 167 | |
| 168 | addr += len; |
| 169 | val += len; |
| 170 | val_size -= len; |
| 171 | } |
| 172 | |
| 173 | while (val_size) { |
| 174 | len = min_t(size_t, val_size, 8); |
| 175 | |
| 176 | err = spmi_ext_register_writel(context, addr, val, len); |
| 177 | if (err) |
| 178 | goto err_out; |
| 179 | |
| 180 | addr += len; |
| 181 | val += len; |
| 182 | val_size -= len; |
| 183 | } |
| 184 | |
| 185 | err_out: |
| 186 | return err; |
| 187 | } |
| 188 | |
| 189 | static int regmap_spmi_ext_write(void *context, const void *data, |
| 190 | size_t count) |
| 191 | { |
| 192 | BUG_ON(count < 2); |
| 193 | return regmap_spmi_ext_gather_write(context, data, 2, data + 2, |
| 194 | count - 2); |
| 195 | } |
| 196 | |
Julia Lawall | 9c2e5cb | 2017-08-11 08:57:36 +0200 | [diff] [blame] | 197 | static const struct regmap_bus regmap_spmi_ext = { |
Josh Cartwright | c9afbb0 | 2014-02-12 13:44:27 -0600 | [diff] [blame] | 198 | .read = regmap_spmi_ext_read, |
| 199 | .write = regmap_spmi_ext_write, |
| 200 | .gather_write = regmap_spmi_ext_gather_write, |
| 201 | .reg_format_endian_default = REGMAP_ENDIAN_NATIVE, |
| 202 | .val_format_endian_default = REGMAP_ENDIAN_NATIVE, |
| 203 | }; |
| 204 | |
Nicolas Boichat | 3cfe7a7 | 2015-07-08 14:30:18 +0800 | [diff] [blame] | 205 | struct regmap *__regmap_init_spmi_ext(struct spmi_device *sdev, |
| 206 | const struct regmap_config *config, |
| 207 | struct lock_class_key *lock_key, |
| 208 | const char *lock_name) |
Josh Cartwright | c9afbb0 | 2014-02-12 13:44:27 -0600 | [diff] [blame] | 209 | { |
Nicolas Boichat | 3cfe7a7 | 2015-07-08 14:30:18 +0800 | [diff] [blame] | 210 | return __regmap_init(&sdev->dev, ®map_spmi_ext, sdev, config, |
| 211 | lock_key, lock_name); |
Josh Cartwright | c9afbb0 | 2014-02-12 13:44:27 -0600 | [diff] [blame] | 212 | } |
Nicolas Boichat | 3cfe7a7 | 2015-07-08 14:30:18 +0800 | [diff] [blame] | 213 | EXPORT_SYMBOL_GPL(__regmap_init_spmi_ext); |
Josh Cartwright | c9afbb0 | 2014-02-12 13:44:27 -0600 | [diff] [blame] | 214 | |
Nicolas Boichat | 3cfe7a7 | 2015-07-08 14:30:18 +0800 | [diff] [blame] | 215 | struct regmap *__devm_regmap_init_spmi_ext(struct spmi_device *sdev, |
| 216 | const struct regmap_config *config, |
| 217 | struct lock_class_key *lock_key, |
| 218 | const char *lock_name) |
Josh Cartwright | a01779f | 2013-10-28 13:12:35 -0500 | [diff] [blame] | 219 | { |
Nicolas Boichat | 3cfe7a7 | 2015-07-08 14:30:18 +0800 | [diff] [blame] | 220 | return __devm_regmap_init(&sdev->dev, ®map_spmi_ext, sdev, config, |
| 221 | lock_key, lock_name); |
Josh Cartwright | a01779f | 2013-10-28 13:12:35 -0500 | [diff] [blame] | 222 | } |
Nicolas Boichat | 3cfe7a7 | 2015-07-08 14:30:18 +0800 | [diff] [blame] | 223 | EXPORT_SYMBOL_GPL(__devm_regmap_init_spmi_ext); |
Josh Cartwright | a01779f | 2013-10-28 13:12:35 -0500 | [diff] [blame] | 224 | |
| 225 | MODULE_LICENSE("GPL"); |