AleX Pelosi | 78a4bea | 2020-09-01 19:02:24 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 2 | /* |
AleX Pelosi | 78a4bea | 2020-09-01 19:02:24 -0700 | [diff] [blame] | 3 | * Support For Battery EEPROM |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 4 | * |
AleX Pelosi | 78a4bea | 2020-09-01 19:02:24 -0700 | [diff] [blame] | 5 | * Copyright 2018 Google, LLC |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 6 | * |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 7 | */ |
| 8 | |
AleX Pelosi | 78a4bea | 2020-09-01 19:02:24 -0700 | [diff] [blame] | 9 | #define pr_fmt(fmt) KBUILD_MODNAME ": %s " fmt, __func__ |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 10 | |
| 11 | #include <linux/kernel.h> |
AleX Pelosi | 2e487e5 | 2021-12-08 17:52:06 -0800 | [diff] [blame] | 12 | #include <linux/slab.h> |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 13 | #include <linux/pm_runtime.h> |
| 14 | #include <linux/nvmem-consumer.h> |
AleX Pelosi | 78a4bea | 2020-09-01 19:02:24 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
Jenny Ho | 18ee632 | 2021-02-04 13:32:00 +0800 | [diff] [blame] | 16 | #include <linux/delay.h> |
AleX Pelosi | 664e21e | 2020-09-01 11:29:51 -0700 | [diff] [blame] | 17 | #include "gbms_storage.h" |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 18 | |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 19 | #define BATT_EEPROM_TAG_MINF_OFFSET 0x00 |
| 20 | #define BATT_EEPROM_TAG_MINF_LEN GBMS_MINF_LEN |
AleX Pelosi | 2e487e5 | 2021-12-08 17:52:06 -0800 | [diff] [blame] | 21 | #define BATT_EEPROM_TAG_BGPN_OFFSET 0x03 |
| 22 | #define BATT_EEPROM_TAG_BGPN_LEN GBMS_BGPN_LEN |
| 23 | #define BATT_EEPROM_TAG_BRID_OFFSET 0x17 |
| 24 | #define BATT_EEPROM_TAG_BRID_LEN 1 |
Jenny Ho | 3341bbb | 2022-01-24 14:49:40 +0800 | [diff] [blame] | 25 | #define BATT_EEPROM_TAG_STRD_OFFSET 0x1E |
| 26 | #define BATT_EEPROM_TAG_STRD_LEN 12 |
Jenny Ho | 1da205c | 2020-11-18 03:58:10 +0800 | [diff] [blame] | 27 | #define BATT_EEPROM_TAG_BCNT_OFFSET 0x2E |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 28 | #define BATT_EEPROM_TAG_BCNT_LEN (GBMS_CCBIN_BUCKET_COUNT * 2) |
Jenny Ho | 1da205c | 2020-11-18 03:58:10 +0800 | [diff] [blame] | 29 | #define BATT_EEPROM_TAG_GMSR_OFFSET 0x42 |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 30 | #define BATT_EEPROM_TAG_GMSR_LEN GBMS_GMSR_LEN |
Jenny Ho | f740ce2 | 2021-06-02 10:15:00 +0800 | [diff] [blame] | 31 | #define BATT_EEPROM_TAG_LOTR_OFFSET 0x59 // Layout Track |
Jenny Ho | 1da205c | 2020-11-18 03:58:10 +0800 | [diff] [blame] | 32 | #define BATT_EEPROM_TAG_LOTR_LEN 1 |
Jenny Ho | 1da205c | 2020-11-18 03:58:10 +0800 | [diff] [blame] | 33 | #define BATT_EEPROM_TAG_CNHS_OFFSET 0x5A |
Jenny Ho | 9e67510 | 2020-10-29 10:38:24 +0800 | [diff] [blame] | 34 | #define BATT_EEPROM_TAG_CNHS_LEN 2 |
Jenny Ho | 9a559fe | 2021-07-06 01:40:31 +0800 | [diff] [blame] | 35 | #define BATT_EEPROM_TAG_SELC_OFFSET 0x5C |
| 36 | #define BATT_EEPROM_TAG_SELC_LEN 1 |
| 37 | #define BATT_EEPROM_TAG_CELC_OFFSET 0x5D |
| 38 | #define BATT_EEPROM_TAG_CELC_LEN 1 |
AleX Pelosi | 2e487e5 | 2021-12-08 17:52:06 -0800 | [diff] [blame] | 39 | |
Jenny Ho | 9a559fe | 2021-07-06 01:40:31 +0800 | [diff] [blame] | 40 | #define BATT_EEPROM_TAG_HIST_OFFSET 0x5E |
Jenny Ho | 1da205c | 2020-11-18 03:58:10 +0800 | [diff] [blame] | 41 | #define BATT_EEPROM_TAG_HIST_LEN BATT_ONE_HIST_LEN |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 42 | |
Jenny Ho | 3341bbb | 2022-01-24 14:49:40 +0800 | [diff] [blame] | 43 | #define BATT_EEPROM_TAG_DINF_OFFSET 0x3FA |
| 44 | #define BATT_EEPROM_TAG_DINF_LEN 4 // GBMS_DINF_LEN |
| 45 | |
AleX Pelosi | bdb1288 | 2021-12-07 09:09:10 -0800 | [diff] [blame] | 46 | static struct gbms_storage_desc *gbee_desc; |
| 47 | |
| 48 | #define GBEE_GET_NVRAM(ptr) ((struct nvmem_device *)(ptr)) |
| 49 | #define GBEE_STORAGE_INFO(tag, addr, count, ptr) \ |
| 50 | (gbee_desc && gbee_desc->info) ? gbee_desc->info(tag, addr, count, ptr) : \ |
| 51 | gbee_storage_info(tag, addr, count, ptr) |
| 52 | |
Jenny Ho | 18ee632 | 2021-02-04 13:32:00 +0800 | [diff] [blame] | 53 | /* |
| 54 | * I2C error when try to write continuous data. |
| 55 | * Add delay before write to wait previous internal write complete |
| 56 | * http://b/179235291#comment8 |
| 57 | */ |
| 58 | #define BATT_WAIT_INTERNAL_WRITE_MS 1 |
| 59 | |
AleX Pelosi | bdb1288 | 2021-12-07 09:09:10 -0800 | [diff] [blame] | 60 | int gbee_storage_info(gbms_tag_t tag, size_t *addr, size_t *count, void *ptr) |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 61 | { |
| 62 | int ret = 0; |
| 63 | |
| 64 | switch (tag) { |
| 65 | case GBMS_TAG_MINF: |
| 66 | *addr = BATT_EEPROM_TAG_MINF_OFFSET; |
| 67 | *count = BATT_EEPROM_TAG_MINF_LEN; |
| 68 | break; |
| 69 | case GBMS_TAG_DINF: |
| 70 | *addr = BATT_EEPROM_TAG_DINF_OFFSET; |
| 71 | *count = BATT_EEPROM_TAG_DINF_LEN; |
| 72 | break; |
| 73 | case GBMS_TAG_HIST: |
| 74 | *addr = BATT_EEPROM_TAG_HIST_OFFSET; |
| 75 | *count = BATT_EEPROM_TAG_HIST_LEN; |
| 76 | break; |
| 77 | case GBMS_TAG_SNUM: |
| 78 | case GBMS_TAG_BGPN: |
| 79 | *addr = BATT_EEPROM_TAG_BGPN_OFFSET; |
| 80 | *count = BATT_EEPROM_TAG_BGPN_LEN; |
| 81 | break; |
| 82 | case GBMS_TAG_GMSR: |
| 83 | *addr = BATT_EEPROM_TAG_GMSR_OFFSET; |
| 84 | *count = BATT_EEPROM_TAG_GMSR_LEN; |
| 85 | break; |
| 86 | case GBMS_TAG_BCNT: |
| 87 | *addr = BATT_EEPROM_TAG_BCNT_OFFSET; |
| 88 | *count = BATT_EEPROM_TAG_BCNT_LEN; |
| 89 | break; |
Jenny Ho | 9e67510 | 2020-10-29 10:38:24 +0800 | [diff] [blame] | 90 | case GBMS_TAG_CNHS: |
| 91 | *addr = BATT_EEPROM_TAG_CNHS_OFFSET; |
| 92 | *count = BATT_EEPROM_TAG_CNHS_LEN; |
| 93 | break; |
Jenny Ho | 1da205c | 2020-11-18 03:58:10 +0800 | [diff] [blame] | 94 | case GBMS_TAG_LOTR: |
| 95 | *addr = BATT_EEPROM_TAG_LOTR_OFFSET; |
| 96 | *count = BATT_EEPROM_TAG_LOTR_LEN; |
| 97 | break; |
Jenny Ho | 9a559fe | 2021-07-06 01:40:31 +0800 | [diff] [blame] | 98 | case GBMS_TAG_SELC: |
| 99 | *addr = BATT_EEPROM_TAG_SELC_OFFSET; |
| 100 | *count = BATT_EEPROM_TAG_SELC_LEN; |
| 101 | break; |
| 102 | case GBMS_TAG_CELC: |
| 103 | *addr = BATT_EEPROM_TAG_CELC_OFFSET; |
| 104 | *count = BATT_EEPROM_TAG_CELC_LEN; |
| 105 | break; |
Jenny Ho | 3341bbb | 2022-01-24 14:49:40 +0800 | [diff] [blame] | 106 | case GBMS_TAG_STRD: |
| 107 | *addr = BATT_EEPROM_TAG_STRD_OFFSET; |
| 108 | *count = BATT_EEPROM_TAG_STRD_LEN; |
| 109 | break; |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 110 | default: |
| 111 | ret = -ENOENT; |
| 112 | break; |
| 113 | } |
| 114 | |
| 115 | return ret; |
| 116 | } |
| 117 | |
| 118 | static int gbee_storage_iter(int index, gbms_tag_t *tag, void *ptr) |
| 119 | { |
AleX Pelosi | bdb1288 | 2021-12-07 09:09:10 -0800 | [diff] [blame] | 120 | static const gbms_tag_t keys[] = { GBMS_TAG_BGPN, GBMS_TAG_MINF, |
| 121 | GBMS_TAG_DINF, GBMS_TAG_HIST, |
| 122 | GBMS_TAG_BRID, GBMS_TAG_SNUM, |
| 123 | GBMS_TAG_GMSR, GBMS_TAG_BCNT, |
| 124 | GBMS_TAG_CNHS, GBMS_TAG_SELC, |
Jenny Ho | 3341bbb | 2022-01-24 14:49:40 +0800 | [diff] [blame] | 125 | GBMS_TAG_CELC, GBMS_TAG_LOTR, |
| 126 | GBMS_TAG_STRD }; |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 127 | const int count = ARRAY_SIZE(keys); |
| 128 | |
| 129 | if (index < 0 || index >= count) |
| 130 | return -ENOENT; |
| 131 | |
| 132 | *tag = keys[index]; |
| 133 | return 0; |
| 134 | } |
| 135 | |
AleX Pelosi | bdb1288 | 2021-12-07 09:09:10 -0800 | [diff] [blame] | 136 | static int gbee_storage_read(gbms_tag_t tag, void *buff, size_t size, void *ptr) |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 137 | { |
AleX Pelosi | bdb1288 | 2021-12-07 09:09:10 -0800 | [diff] [blame] | 138 | struct nvmem_device *nvmem = GBEE_GET_NVRAM(ptr); |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 139 | size_t offset = 0, len = 0; |
| 140 | int ret; |
| 141 | |
| 142 | if (tag == GBMS_TAG_BRID) { |
| 143 | u8 temp; |
| 144 | |
| 145 | if (size != sizeof(u32)) |
| 146 | return -ENOMEM; |
| 147 | |
| 148 | ret = nvmem_device_read(nvmem, BATT_EEPROM_TAG_BRID_OFFSET, |
| 149 | 1, &temp); |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 150 | if (ret < 0) |
| 151 | return ret; |
| 152 | |
| 153 | ((u32*)buff)[0] = temp; |
| 154 | return len; |
| 155 | } |
| 156 | |
AleX Pelosi | bdb1288 | 2021-12-07 09:09:10 -0800 | [diff] [blame] | 157 | ret = GBEE_STORAGE_INFO(tag, &offset, &len, ptr); |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 158 | if (ret < 0) |
| 159 | return ret; |
| 160 | if (!len) |
| 161 | return -ENOENT; |
| 162 | if (len > size) |
| 163 | return -ENOMEM; |
| 164 | |
| 165 | ret = nvmem_device_read(nvmem, offset, len, buff); |
| 166 | if (ret == 0) |
| 167 | ret = len; |
| 168 | |
| 169 | return ret; |
| 170 | } |
| 171 | |
| 172 | static int gbee_storage_write(gbms_tag_t tag, const void *buff, size_t size, |
| 173 | void *ptr) |
| 174 | { |
| 175 | struct nvmem_device *nvmem = ptr; |
| 176 | size_t offset = 0, len = 0; |
Jenny Ho | 18ee632 | 2021-02-04 13:32:00 +0800 | [diff] [blame] | 177 | int ret, write_size = 0; |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 178 | |
| 179 | if ((tag != GBMS_TAG_DINF) && (tag != GBMS_TAG_GMSR) && |
Jenny Ho | 9a559fe | 2021-07-06 01:40:31 +0800 | [diff] [blame] | 180 | (tag != GBMS_TAG_BCNT) && (tag != GBMS_TAG_CNHS) && |
Jack Wu | 5ac3992 | 2021-10-29 17:53:24 +0800 | [diff] [blame] | 181 | (tag != GBMS_TAG_SELC) && (tag != GBMS_TAG_CELC) && |
Jenny Ho | 3341bbb | 2022-01-24 14:49:40 +0800 | [diff] [blame] | 182 | (tag != GBMS_TAG_BPST) && (tag != GBMS_TAG_STRD)) |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 183 | return -ENOENT; |
| 184 | |
AleX Pelosi | bdb1288 | 2021-12-07 09:09:10 -0800 | [diff] [blame] | 185 | ret = GBEE_STORAGE_INFO(tag, &offset, &len, ptr); |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 186 | if (ret < 0) |
| 187 | return ret; |
| 188 | if (size > len) |
| 189 | return -ENOMEM; |
| 190 | |
Jenny Ho | 18ee632 | 2021-02-04 13:32:00 +0800 | [diff] [blame] | 191 | for (write_size = 0; write_size < size; write_size++) { |
| 192 | ret = nvmem_device_write(nvmem, write_size + offset, 1, |
| 193 | &((char *)buff)[write_size]); |
| 194 | if (ret < 0) |
| 195 | return ret; |
| 196 | msleep(BATT_WAIT_INTERNAL_WRITE_MS); |
| 197 | } |
| 198 | |
| 199 | ret = size; |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 200 | |
| 201 | return ret; |
| 202 | } |
| 203 | |
| 204 | static int gbee_storage_read_data(gbms_tag_t tag, void *data, size_t count, |
| 205 | int idx, void *ptr) |
| 206 | { |
AleX Pelosi | bdb1288 | 2021-12-07 09:09:10 -0800 | [diff] [blame] | 207 | struct nvmem_device *nvmem = GBEE_GET_NVRAM(ptr); |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 208 | size_t offset = 0, len = 0; |
| 209 | int ret; |
| 210 | |
| 211 | switch (tag) { |
| 212 | case GBMS_TAG_HIST: |
AleX Pelosi | bdb1288 | 2021-12-07 09:09:10 -0800 | [diff] [blame] | 213 | ret = GBEE_STORAGE_INFO(tag, &offset, &len, ptr); |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 214 | break; |
| 215 | default: |
| 216 | ret = -ENOENT; |
| 217 | break; |
| 218 | } |
| 219 | |
| 220 | if (ret < 0) |
| 221 | return ret; |
| 222 | |
| 223 | if (!data || !count) { |
| 224 | if (idx == GBMS_STORAGE_INDEX_INVALID) |
| 225 | return 0; |
| 226 | else |
| 227 | return BATT_MAX_HIST_CNT; |
| 228 | } |
| 229 | |
| 230 | if (idx < 0) |
| 231 | return -EINVAL; |
| 232 | |
| 233 | /* index == 0 is ok here */ |
| 234 | if (idx >= BATT_MAX_HIST_CNT) |
| 235 | return -ENODATA; |
| 236 | |
| 237 | if (len > count) |
| 238 | return -EINVAL; |
| 239 | |
| 240 | offset += len * idx; |
| 241 | |
| 242 | ret = nvmem_device_read(nvmem, offset, len, data); |
| 243 | if (ret == 0) |
| 244 | ret = len; |
| 245 | |
| 246 | return ret; |
| 247 | } |
| 248 | |
| 249 | static int gbee_storage_write_data(gbms_tag_t tag, const void *data, |
| 250 | size_t count, int idx, void *ptr) |
| 251 | { |
AleX Pelosi | bdb1288 | 2021-12-07 09:09:10 -0800 | [diff] [blame] | 252 | struct nvmem_device *nvmem = GBEE_GET_NVRAM(ptr); |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 253 | size_t offset = 0, len = 0; |
Jenny Ho | 18ee632 | 2021-02-04 13:32:00 +0800 | [diff] [blame] | 254 | int ret, write_size = 0; |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 255 | |
| 256 | switch (tag) { |
| 257 | case GBMS_TAG_HIST: |
AleX Pelosi | bdb1288 | 2021-12-07 09:09:10 -0800 | [diff] [blame] | 258 | ret = GBEE_STORAGE_INFO(tag, &offset, &len, ptr); |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 259 | break; |
| 260 | default: |
| 261 | ret = -ENOENT; |
| 262 | break; |
| 263 | } |
| 264 | |
| 265 | if (ret < 0) |
| 266 | return ret; |
| 267 | |
| 268 | if (idx < 0 || !data || !count) |
| 269 | return -EINVAL; |
| 270 | |
| 271 | /* index == 0 is ok here */ |
| 272 | if (idx >= BATT_MAX_HIST_CNT) |
| 273 | return -ENODATA; |
| 274 | |
| 275 | if (count > len) |
| 276 | return -EINVAL; |
| 277 | |
| 278 | offset += len * idx; |
| 279 | |
Jenny Ho | 18ee632 | 2021-02-04 13:32:00 +0800 | [diff] [blame] | 280 | for (write_size = 0; write_size < len; write_size++) { |
| 281 | ret = nvmem_device_write(nvmem, write_size + offset, 1, |
| 282 | &((char *)data)[write_size]); |
| 283 | if (ret < 0) |
| 284 | return ret; |
| 285 | msleep(BATT_WAIT_INTERNAL_WRITE_MS); |
| 286 | } |
| 287 | |
| 288 | ret = len; |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 289 | |
| 290 | return ret; |
| 291 | } |
| 292 | |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 293 | static struct gbms_storage_desc gbee_storage_dsc = { |
| 294 | .info = gbee_storage_info, |
| 295 | .iter = gbee_storage_iter, |
| 296 | .read = gbee_storage_read, |
| 297 | .write = gbee_storage_write, |
| 298 | .read_data = gbee_storage_read_data, |
| 299 | .write_data = gbee_storage_write_data, |
| 300 | }; |
| 301 | |
AleX Pelosi | bdb1288 | 2021-12-07 09:09:10 -0800 | [diff] [blame] | 302 | struct gbms_storage_desc gbee_storage01_dsc = { |
| 303 | .info = gbee_storage01_info, |
| 304 | .iter = gbee_storage01_iter, |
| 305 | .read = gbee_storage_read, |
| 306 | .write = gbee_storage_write, |
| 307 | .read_data = gbee_storage_read_data, |
| 308 | .write_data = gbee_storage_write_data, |
| 309 | }; |
| 310 | |
AleX Pelosi | 2e487e5 | 2021-12-08 17:52:06 -0800 | [diff] [blame] | 311 | /* TODO: factor history mechanics out of google battery? */ |
| 312 | static int gbms_hist_move(struct nvmem_device *nvmem, int from, int to, int len) |
AleX Pelosi | bdb1288 | 2021-12-07 09:09:10 -0800 | [diff] [blame] | 313 | { |
AleX Pelosi | 2e487e5 | 2021-12-08 17:52:06 -0800 | [diff] [blame] | 314 | u8 *buff, *p; |
| 315 | int index, ret; |
| 316 | |
| 317 | buff = kzalloc(len, GFP_KERNEL); |
| 318 | if (!buff) |
| 319 | return -ENOMEM; |
| 320 | |
| 321 | /* move only the entries that are used */ |
| 322 | p = buff; |
| 323 | for (index = 0; index < BATT_MAX_HIST_CNT; index++) { |
| 324 | ret = nvmem_device_read(nvmem, from, BATT_ONE_HIST_LEN, p); |
| 325 | if (ret < 0) { |
| 326 | pr_err("%s: cannot read history data (%d)\n", __func__, ret); |
| 327 | goto exit; |
| 328 | } |
| 329 | |
| 330 | /* verify 1st byte for tempco */ |
| 331 | if (*p == 0xff) |
| 332 | break; |
| 333 | /* move to next history entry */ |
| 334 | from += BATT_ONE_HIST_LEN; |
| 335 | p += BATT_ONE_HIST_LEN; |
| 336 | } |
| 337 | |
| 338 | ret = nvmem_device_write(nvmem, to, (BATT_ONE_HIST_LEN * index), buff); |
| 339 | if (ret < 0) |
| 340 | pr_err("%s: cannot write history data (%d)\n", __func__, ret); |
| 341 | |
| 342 | exit: |
| 343 | kfree(buff); |
| 344 | return ret; |
| 345 | } |
| 346 | |
| 347 | /* LOTR is in a fixed position, move */ |
| 348 | static int gbms_lotr_update(struct nvmem_device *nvmem, int lotr_to) |
| 349 | { |
| 350 | int ret, lotr_from = 0; |
| 351 | static u8 init_data[5]= { 0 }; |
AleX Pelosi | bdb1288 | 2021-12-07 09:09:10 -0800 | [diff] [blame] | 352 | |
| 353 | ret = nvmem_device_read(nvmem, BATT_EEPROM_TAG_LOTR_OFFSET, |
AleX Pelosi | 2e487e5 | 2021-12-08 17:52:06 -0800 | [diff] [blame] | 354 | BATT_EEPROM_TAG_LOTR_LEN, &lotr_from); |
| 355 | if (ret < 0 || lotr_from == lotr_to) |
AleX Pelosi | bdb1288 | 2021-12-07 09:09:10 -0800 | [diff] [blame] | 356 | return ret; |
| 357 | |
AleX Pelosi | 2e487e5 | 2021-12-08 17:52:06 -0800 | [diff] [blame] | 358 | if (lotr_to != GBMS_LOTR_V1 || lotr_from != GBMS_LOTR_DEFAULT) |
| 359 | return 0; |
| 360 | |
| 361 | ret = gbms_hist_move(nvmem, 0x5E, 0x64, BATT_TOTAL_HIST_LEN); |
| 362 | if (ret < 0) { |
| 363 | pr_err("%s: cannot move history\n", __func__); |
| 364 | return ret; |
| 365 | /* TODO: flag this in BPST? */ |
| 366 | } |
| 367 | |
| 368 | ret = nvmem_device_write(nvmem, 0x5E, sizeof(init_data), init_data); |
| 369 | if (ret != sizeof(init_data)) { |
| 370 | pr_err("%s: cannot init new fields\n", __func__); |
| 371 | return ret < 0 ? ret : -EINVAL; |
| 372 | } |
| 373 | |
| 374 | /* TODO: how do we handle backporting? */ |
AleX Pelosi | bdb1288 | 2021-12-07 09:09:10 -0800 | [diff] [blame] | 375 | |
| 376 | /* now write lotr to the right place */ |
AleX Pelosi | 2e487e5 | 2021-12-08 17:52:06 -0800 | [diff] [blame] | 377 | ret = nvmem_device_write(nvmem, BATT_EEPROM_TAG_LOTR_OFFSET, |
| 378 | BATT_EEPROM_TAG_LOTR_LEN, &lotr_to); |
| 379 | if (ret == BATT_EEPROM_TAG_LOTR_LEN) |
| 380 | pr_info("%s: lotr migrated %d->%d\n", __func__, lotr_from, lotr_to); |
| 381 | |
| 382 | return ret; |
AleX Pelosi | bdb1288 | 2021-12-07 09:09:10 -0800 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | static struct gbms_storage_desc *gbms_lotr_2_dsc(int lotr_ver) |
| 386 | { |
| 387 | switch (lotr_ver) { |
| 388 | case GBMS_LOTR_V1: |
| 389 | return &gbee_storage01_dsc; |
| 390 | default: |
| 391 | return &gbee_storage_dsc; |
| 392 | } |
| 393 | } |
| 394 | |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 395 | /* |
| 396 | * Caller will use something like of_nvmem_device_get() to retrieve the |
| 397 | * nvmem_device instance. |
AleX Pelosi | bdb1288 | 2021-12-07 09:09:10 -0800 | [diff] [blame] | 398 | * TODO: this only supports a singleton but the model can be extended to |
| 399 | * multiple eeproms passing a structure to gbms_storage_register() and |
| 400 | * modifying the implementation of GBEE_GET_NVRAM and GBEE_STORAGE_INFO |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 401 | * TODO: map nvram cells to tags |
| 402 | */ |
AleX Pelosi | bdb1288 | 2021-12-07 09:09:10 -0800 | [diff] [blame] | 403 | int gbee_register_device(const char *name, int lotr, struct nvmem_device *nvram) |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 404 | { |
AleX Pelosi | bdb1288 | 2021-12-07 09:09:10 -0800 | [diff] [blame] | 405 | int ret; |
| 406 | |
| 407 | gbee_desc = gbms_lotr_2_dsc(lotr); |
| 408 | if (!gbee_desc) |
| 409 | return -EINVAL; |
| 410 | |
| 411 | /* convert the layout (if needed) */ |
| 412 | ret = gbms_lotr_update(nvram, lotr); |
| 413 | if (ret < 0) { |
| 414 | pr_err("gbee %s update lotr failed, %d\n", name, ret); |
| 415 | goto error_exit; |
| 416 | } |
| 417 | |
| 418 | /* watch out for races on gbee_desc */ |
| 419 | ret = gbms_storage_register(gbee_desc, name, nvram); |
| 420 | if (ret == 0) |
| 421 | return 0; |
| 422 | |
| 423 | error_exit: |
| 424 | gbee_desc = NULL; |
| 425 | return ret; |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 426 | } |
AleX Pelosi | 78a4bea | 2020-09-01 19:02:24 -0700 | [diff] [blame] | 427 | EXPORT_SYMBOL_GPL(gbee_register_device); |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 428 | |
| 429 | void gbee_destroy_device(void) |
| 430 | { |
| 431 | |
| 432 | } |
AleX Pelosi | 78a4bea | 2020-09-01 19:02:24 -0700 | [diff] [blame] | 433 | EXPORT_SYMBOL_GPL(gbee_destroy_device); |
| 434 | |
| 435 | MODULE_AUTHOR("AleX Pelosi <apelosi@google.com>"); |
AleX Pelosi | bdb1288 | 2021-12-07 09:09:10 -0800 | [diff] [blame] | 436 | MODULE_DESCRIPTION("Google EEPROM"); |
AleX Pelosi | 78a4bea | 2020-09-01 19:02:24 -0700 | [diff] [blame] | 437 | MODULE_LICENSE("GPL"); |