| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include <console/console.h> |
| 19 |
#include <delay.h> |
| 20 |
#include <device/i2c_simple.h> |
| 21 |
#include <string.h> |
| 22 |
|
| 23 |
#include "drivers/i2c/ww_ring/ww_ring_programs.h" |
| 24 |
|
| 25 |
|
| 26 |
#define WW_RING_BASE_ADDR 0x32 |
| 27 |
|
| 28 |
|
| 29 |
#define LP55231_ENGCTRL1_REG 0x00 |
| 30 |
#define LP55231_ENGCTRL2_REG 0x01 |
| 31 |
#define LP55231_D1_CRT_CTRL_REG 0x26 |
| 32 |
#define LP55231_MISC_REG 0x36 |
| 33 |
#define LP55231_VARIABLE_REG 0x3c |
| 34 |
#define LP55231_RESET_REG 0x3d |
| 35 |
#define LP55231_ENG1_PROG_START 0x4c |
| 36 |
#define LP55231_PROG_PAGE_REG 0x4f |
| 37 |
#define LP55231_PROG_BASE_REG 0x50 |
| 38 |
|
| 39 |
|
| 40 |
#define LP55231_CRT_CTRL_DEFAULT 0xaf |
| 41 |
|
| 42 |
|
| 43 |
#define LP55231_ENGCTRL1_CHIP_EN 0x40 |
| 44 |
#define LP55231_ENGCTRL1_ALL_ENG_GO 0x2a |
| 45 |
|
| 46 |
|
| 47 |
#define LP55231_ENGCTRL2_ALL_DISABLE 0 |
| 48 |
#define LP55231_ENGCTRL2_ALL_LOAD 0x15 |
| 49 |
#define LP55231_ENGCTRL2_ALL_RUN 0x2a |
| 50 |
|
| 51 |
|
| 52 |
#define LP55231_MISC_AUTOINCR (1 << 6) |
| 53 |
#define LP55231_MISC_PUMP_1X (1 << 3) |
| 54 |
#define LP55231_MISC_INT_CLK (3 << 0) |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
#define LP55231_VARIABLE_COOKIE 0xb4 |
| 61 |
|
| 62 |
|
| 63 |
#define LP55231_RESET_VALUE 0xff |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
#define LP55231_PROG_PAGE_SIZE 32 |
| 70 |
#define LP55231_PROG_PAGES 6 |
| 71 |
#define LP55231_MAX_PROG_SIZE (LP55231_PROG_PAGE_SIZE * LP55231_PROG_PAGES) |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
typedef struct { |
| 80 |
unsigned int i2c_bus; |
| 81 |
uint8_t dev_addr; |
| 82 |
uint8_t data_buffer[LP55231_PROG_PAGE_SIZE + 1]; |
| 83 |
} TiLp55231; |
| 84 |
|
| 85 |
static void ww_ring_init(unsigned int i2c_bus); |
| 86 |
|
| 87 |
|
| 88 |
static TiLp55231 lp55231s[WW_RING_NUM_LED_CONTROLLERS]; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
static int ledc_transfer(TiLp55231 *ledc, struct i2c_msg *segs, |
| 104 |
int seg_count, int reset) |
| 105 |
{ |
| 106 |
int rv, max_attempts = 2; |
| 107 |
|
| 108 |
while (max_attempts--) { |
| 109 |
rv = i2c_transfer(ledc->i2c_bus, segs, seg_count); |
| 110 |
|
| 111 |
|
| 112 |
if (!rv || reset) |
| 113 |
break; |
| 114 |
} |
| 115 |
|
| 116 |
if (rv) { |
| 117 |
if (!reset) |
| 118 |
printk(BIOS_WARNING, |
| 119 |
"%s: dev %#x, reg %#x, %s transaction error.\n", |
| 120 |
__func__, segs->slave, segs->buf[0], |
| 121 |
seg_count == 1 ? "write" : "read"); |
| 122 |
else |
| 123 |
rv = 0; |
| 124 |
} |
| 125 |
|
| 126 |
return rv; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
static int ledc_write(TiLp55231 *ledc, uint8_t start_addr, |
| 134 |
const uint8_t *data, unsigned int count) |
| 135 |
{ |
| 136 |
struct i2c_msg seg; |
| 137 |
|
| 138 |
if (count > (sizeof(ledc->data_buffer) - 1)) { |
| 139 |
printk(BIOS_WARNING, "%s: transfer size too large (%d bytes)\n", |
| 140 |
__func__, count); |
| 141 |
return -1; |
| 142 |
} |
| 143 |
|
| 144 |
memcpy(ledc->data_buffer + 1, data, count); |
| 145 |
ledc->data_buffer[0] = start_addr; |
| 146 |
|
| 147 |
seg.flags = 0; |
| 148 |
seg.slave = ledc->dev_addr; |
| 149 |
seg.buf = ledc->data_buffer; |
| 150 |
seg.len = count + 1; |
| 151 |
|
| 152 |
return ledc_transfer(ledc, &seg, 1, start_addr == LP55231_RESET_REG); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
static int ledc_read(TiLp55231 *ledc, uint8_t addr, uint8_t *data) |
| 157 |
{ |
| 158 |
struct i2c_msg seg[2]; |
| 159 |
|
| 160 |
seg[0].flags = 0; |
| 161 |
seg[0].slave = ledc->dev_addr; |
| 162 |
seg[0].buf = &addr; |
| 163 |
seg[0].len = 1; |
| 164 |
|
| 165 |
seg[1].flags = I2C_M_RD; |
| 166 |
seg[1].slave = ledc->dev_addr; |
| 167 |
seg[1].buf = data; |
| 168 |
seg[1].len = 1; |
| 169 |
|
| 170 |
return ledc_transfer(ledc, seg, ARRAY_SIZE(seg), |
| 171 |
addr == LP55231_RESET_REG); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
static int ledc_reset(TiLp55231 *ledc) |
| 182 |
{ |
| 183 |
uint8_t data; |
| 184 |
|
| 185 |
data = ~0; |
| 186 |
ledc_read(ledc, LP55231_RESET_REG, &data); |
| 187 |
if (data) { |
| 188 |
printk(BIOS_WARNING, |
| 189 |
"WW_RING: no controller found at address %#2.2x\n", |
| 190 |
ledc->dev_addr); |
| 191 |
return -1; |
| 192 |
} |
| 193 |
|
| 194 |
data = LP55231_RESET_VALUE; |
| 195 |
ledc_write(ledc, LP55231_RESET_REG, &data, 1); |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
ledc_read(ledc, LP55231_RESET_REG, &data); |
| 204 |
return 0; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
static void ledc_write_program(TiLp55231 *ledc, uint8_t load_addr, |
| 212 |
const uint8_t *program, unsigned int count) |
| 213 |
{ |
| 214 |
uint8_t page_num = load_addr / LP55231_PROG_PAGE_SIZE; |
| 215 |
unsigned int page_offs = load_addr % LP55231_PROG_PAGE_SIZE; |
| 216 |
|
| 217 |
if ((load_addr + count) > LP55231_MAX_PROG_SIZE) { |
| 218 |
printk(BIOS_WARNING, |
| 219 |
"%s: program of size %#x does not fit at addr %#x\n", |
| 220 |
__func__, count, load_addr); |
| 221 |
return; |
| 222 |
} |
| 223 |
|
| 224 |
while (count) { |
| 225 |
unsigned int segment_size = LP55231_PROG_PAGE_SIZE - page_offs; |
| 226 |
|
| 227 |
if (segment_size > count) |
| 228 |
segment_size = count; |
| 229 |
|
| 230 |
ledc_write(ledc, LP55231_PROG_PAGE_REG, &page_num, 1); |
| 231 |
ledc_write(ledc, LP55231_PROG_BASE_REG + page_offs, |
| 232 |
program, segment_size); |
| 233 |
|
| 234 |
count -= segment_size; |
| 235 |
program += segment_size; |
| 236 |
page_offs = 0; |
| 237 |
page_num++; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
static void ledc_write_engctrl2(TiLp55231 *ledc, uint8_t value) |
| 242 |
{ |
| 243 |
ledc_write(ledc, LP55231_ENGCTRL2_REG, &value, 1); |
| 244 |
udelay(1500); |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
static void ledc_run_program(TiLp55231 *ledc, |
| 249 |
const TiLp55231Program *program_desc) |
| 250 |
{ |
| 251 |
int i; |
| 252 |
uint8_t data; |
| 253 |
|
| 254 |
|
| 255 |
data = LP55231_ENGCTRL1_CHIP_EN; |
| 256 |
ledc_write(ledc, LP55231_ENGCTRL1_REG, &data, 1); |
| 257 |
|
| 258 |
ledc_write_engctrl2(ledc, LP55231_ENGCTRL2_ALL_DISABLE); |
| 259 |
ledc_write_engctrl2(ledc, LP55231_ENGCTRL2_ALL_LOAD); |
| 260 |
|
| 261 |
ledc_write_program(ledc, program_desc->load_addr, |
| 262 |
program_desc->program_text, |
| 263 |
program_desc->program_size); |
| 264 |
|
| 265 |
for (i = 0; i < sizeof(program_desc->engine_start_addr); i++) |
| 266 |
ledc_write(ledc, LP55231_ENG1_PROG_START + i, |
| 267 |
program_desc->engine_start_addr + i, 1); |
| 268 |
|
| 269 |
data = LP55231_ENGCTRL1_CHIP_EN | LP55231_ENGCTRL1_ALL_ENG_GO; |
| 270 |
ledc_write(ledc, LP55231_ENGCTRL1_REG, &data, 1); |
| 271 |
ledc_write_engctrl2(ledc, LP55231_ENGCTRL2_ALL_RUN); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
static int ledc_init_validate(TiLp55231 *ledc) |
| 279 |
{ |
| 280 |
uint8_t data; |
| 281 |
int i; |
| 282 |
|
| 283 |
if (ledc_reset(ledc)) |
| 284 |
return -1; |
| 285 |
|
| 286 |
|
| 287 |
data = LP55231_ENGCTRL1_CHIP_EN; |
| 288 |
ledc_write(ledc, LP55231_ENGCTRL1_REG, &data, 1); |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
data = LP55231_MISC_AUTOINCR | |
| 295 |
LP55231_MISC_PUMP_1X | LP55231_MISC_INT_CLK; |
| 296 |
ledc_write(ledc, LP55231_MISC_REG, &data, 1); |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
for (i = 0; i < 9; i++) { |
| 303 |
ledc_read(ledc, LP55231_D1_CRT_CTRL_REG + i, &data); |
| 304 |
if (data != LP55231_CRT_CTRL_DEFAULT) { |
| 305 |
printk(BIOS_WARNING, |
| 306 |
"%s: read %#2.2x from register %#x\n", __func__, |
| 307 |
data, LP55231_D1_CRT_CTRL_REG + i); |
| 308 |
return -1; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
data = LP55231_VARIABLE_COOKIE; |
| 317 |
ledc_write(ledc, LP55231_VARIABLE_REG, &data, 1); |
| 318 |
|
| 319 |
return 0; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
int ww_ring_display_pattern(unsigned int i2c_bus, enum display_pattern pattern) |
| 327 |
{ |
| 328 |
static int initted; |
| 329 |
const WwRingStateProg *wwr_prog; |
| 330 |
|
| 331 |
if (!initted) { |
| 332 |
ww_ring_init(i2c_bus); |
| 333 |
initted = 1; |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
for (wwr_prog = wwr_state_programs; wwr_prog->programs[0]; wwr_prog++) |
| 338 |
if (wwr_prog->led_pattern == pattern) { |
| 339 |
int j; |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
for (j = 0; j < WW_RING_NUM_LED_CONTROLLERS; j++) { |
| 346 |
if (!lp55231s[j].dev_addr) |
| 347 |
continue; |
| 348 |
ledc_write_engctrl2 |
| 349 |
(lp55231s + j, |
| 350 |
LP55231_ENGCTRL2_ALL_DISABLE); |
| 351 |
} |
| 352 |
|
| 353 |
for (j = 0; j < WW_RING_NUM_LED_CONTROLLERS; j++) { |
| 354 |
if (!lp55231s[j].dev_addr) |
| 355 |
continue; |
| 356 |
ledc_run_program(lp55231s + j, |
| 357 |
wwr_prog->programs[j]); |
| 358 |
} |
| 359 |
return 0; |
| 360 |
} |
| 361 |
|
| 362 |
printk(BIOS_WARNING, "%s: did not find program for pattern %d\n", |
| 363 |
__func__, pattern); |
| 364 |
|
| 365 |
return -1; |
| 366 |
} |
| 367 |
|
| 368 |
#define LP55231_I2C_BASE_ADDR 0x32 |
| 369 |
|
| 370 |
static void ww_ring_init(unsigned int i2c_bus) |
| 371 |
{ |
| 372 |
TiLp55231 *ledc; |
| 373 |
int i, count = 0; |
| 374 |
|
| 375 |
for (i = 0, ledc = lp55231s; |
| 376 |
i < WW_RING_NUM_LED_CONTROLLERS; |
| 377 |
i++, ledc++) { |
| 378 |
ledc->i2c_bus = i2c_bus; |
| 379 |
ledc->dev_addr = LP55231_I2C_BASE_ADDR + i; |
| 380 |
|
| 381 |
if (!ledc_init_validate(ledc)) |
| 382 |
count++; |
| 383 |
else |
| 384 |
ledc->dev_addr = 0; |
| 385 |
} |
| 386 |
|
| 387 |
printk(BIOS_INFO, "WW_RING: initialized %d out of %d\n", count, i); |
| 388 |
if (count != i) { |
| 389 |
if (count) |
| 390 |
printk(BIOS_WARNING, |
| 391 |
"WW_RING: will keep going anyway\n"); |
| 392 |
else |
| 393 |
printk(BIOS_WARNING, |
| 394 |
"WW_RING: LED ring not present\n"); |
| 395 |
} |
| 396 |
} |