1 /* This file is part of the coreboot project. */
2 /* SPDX-License-Identifier: GPL-2.0-only */
5 * This is a driver for the Whirlwind LED ring, which is equipped with two LED
6 * microcontrollers TI LP55231 (http://www.ti.com/product/lp55231), each of
7 * them driving three multicolor LEDs.
9 * The only connection between the ring and the main board is an i2c bus.
11 * This driver imitates a depthcharge display device. On initialization the
12 * driver sets up the controllers to prepare them to accept programs to run.
14 * When a certain vboot state needs to be indicated, the program for that
15 * state is loaded into the controllers, resulting in the state appropriate
19 #include <console/console.h>
21 #include <device/i2c_simple.h>
24 #include "drivers/i2c/ww_ring/ww_ring_programs.h"
26 /* I2c address of the first of the controllers, the rest are contiguous. */
27 #define WW_RING_BASE_ADDR 0x32
29 /* Key lp55231 registers. */
30 #define LP55231_ENGCTRL1_REG 0x00
31 #define LP55231_ENGCTRL2_REG 0x01
32 #define LP55231_D1_CRT_CTRL_REG 0x26
33 #define LP55231_MISC_REG 0x36
34 #define LP55231_VARIABLE_REG 0x3c
35 #define LP55231_RESET_REG 0x3d
36 #define LP55231_ENG1_PROG_START 0x4c
37 #define LP55231_PROG_PAGE_REG 0x4f
38 #define LP55231_PROG_BASE_REG 0x50
40 /* LP55231_D1_CRT_CTRL_REG, default value, applies to all nine of them */
41 #define LP55231_CRT_CTRL_DEFAULT 0xaf
43 /* LP55231_ENGCTRL1_REG fields */
44 #define LP55231_ENGCTRL1_CHIP_EN 0x40
45 #define LP55231_ENGCTRL1_ALL_ENG_GO 0x2a
47 /* LP55231_ENGCTRL2_REG fields. */
48 #define LP55231_ENGCTRL2_ALL_DISABLE 0
49 #define LP55231_ENGCTRL2_ALL_LOAD 0x15
50 #define LP55231_ENGCTRL2_ALL_RUN 0x2a
52 /* LP55231_MISC_REG fields. */
53 #define LP55231_MISC_AUTOINCR (1 << 6)
54 #define LP55231_MISC_PUMP_1X (1 << 3)
55 #define LP55231_MISC_INT_CLK (3 << 0)
58 * LP55231_VARIABLE_REG cookie value. It indicates to depthcharge that the
59 * ring has been initialized by coreboot.
61 #define LP55231_VARIABLE_COOKIE 0xb4
63 /* Goes into LP55231_RESET_REG to reset the chip. */
64 #define LP55231_RESET_VALUE 0xff
67 * The controller has 192 bytes of SRAM for code/data, available as six 32 byte
70 #define LP55231_PROG_PAGE_SIZE 32
71 #define LP55231_PROG_PAGES 6
72 #define LP55231_MAX_PROG_SIZE (LP55231_PROG_PAGE_SIZE * LP55231_PROG_PAGES)
75 * Structure to cache data relevant to accessing one controller. I2c interface
76 * to use, device address on the i2c bus and a data buffer for write
77 * transactions. The most bytes sent at a time is the register address plus
78 * the program page size.
83 uint8_t data_buffer
[LP55231_PROG_PAGE_SIZE
+ 1];
86 static void ww_ring_init(unsigned int i2c_bus
);
88 /* Controller descriptors. */
89 static TiLp55231 lp55231s
[WW_RING_NUM_LED_CONTROLLERS
];
92 * i2c transfer function for the driver. To keep things simple, the function
93 * repeats the transfer, if the first attempt fails. This is OK with the
94 * controller and makes it easier to handle errors.
96 * Note that the reset register accesses are expected to fail on writes, but
97 * due to a bug in the ipq806x i2c controller, the error is reported on the
98 * following read attempt.
100 * To work around this the driver writes and then reads the reset register,
101 * the transfer function ignores errors when accessing the reset register.
104 static int ledc_transfer(TiLp55231
*ledc
, struct i2c_msg
*segs
,
105 int seg_count
, int reset
)
107 int rv
, max_attempts
= 2;
109 while (max_attempts
--) {
110 rv
= i2c_transfer(ledc
->i2c_bus
, segs
, seg_count
);
112 /* Accessing reset register is expected to fail. */
120 "%s: dev %#x, reg %#x, %s transaction error.\n",
121 __func__
, segs
->slave
, segs
->buf
[0],
122 seg_count
== 1 ? "write" : "read");
131 * The controller is programmed to autoincrement on writes, so up to page size
132 * bytes can be transmitted in one write transaction.
134 static int ledc_write(TiLp55231
*ledc
, uint8_t start_addr
,
135 const uint8_t *data
, unsigned int count
)
139 if (count
> (sizeof(ledc
->data_buffer
) - 1)) {
140 printk(BIOS_WARNING
, "%s: transfer size too large (%d bytes)\n",
145 memcpy(ledc
->data_buffer
+ 1, data
, count
);
146 ledc
->data_buffer
[0] = start_addr
;
149 seg
.slave
= ledc
->dev_addr
;
150 seg
.buf
= ledc
->data_buffer
;
153 return ledc_transfer(ledc
, &seg
, 1, start_addr
== LP55231_RESET_REG
);
156 /* To keep things simple, read is limited to one byte at a time. */
157 static int ledc_read(TiLp55231
*ledc
, uint8_t addr
, uint8_t *data
)
159 struct i2c_msg seg
[2];
162 seg
[0].slave
= ledc
->dev_addr
;
166 seg
[1].flags
= I2C_M_RD
;
167 seg
[1].slave
= ledc
->dev_addr
;
171 return ledc_transfer(ledc
, seg
, ARRAY_SIZE(seg
),
172 addr
== LP55231_RESET_REG
);
176 * Reset transaction is expected to result in a failing i2c command. But even
177 * before trying it, read the reset register, which is supposed to always
178 * return 0. If this fails - there is no lp55231 at this address.
180 * Return 0 on success, -1 on failure to detect controller.
182 static int ledc_reset(TiLp55231
*ledc
)
187 ledc_read(ledc
, LP55231_RESET_REG
, &data
);
190 "WW_RING: no controller found at address %#2.2x\n",
195 data
= LP55231_RESET_VALUE
;
196 ledc_write(ledc
, LP55231_RESET_REG
, &data
, 1);
199 * This read is not necessary for the chip reset, but is required to
200 * work around the i2c driver bug where the missing ACK on the last
201 * byte of the write transaction is ignored, but the next transaction
204 ledc_read(ledc
, LP55231_RESET_REG
, &data
);
209 * Write a program into the internal lp55231 memory. Split write transactions
210 * into sections fitting into memory pages.
212 static void ledc_write_program(TiLp55231
*ledc
, uint8_t load_addr
,
213 const uint8_t *program
, unsigned int count
)
215 uint8_t page_num
= load_addr
/ LP55231_PROG_PAGE_SIZE
;
216 unsigned int page_offs
= load_addr
% LP55231_PROG_PAGE_SIZE
;
218 if ((load_addr
+ count
) > LP55231_MAX_PROG_SIZE
) {
220 "%s: program of size %#x does not fit at addr %#x\n",
221 __func__
, count
, load_addr
);
226 unsigned int segment_size
= LP55231_PROG_PAGE_SIZE
- page_offs
;
228 if (segment_size
> count
)
229 segment_size
= count
;
231 ledc_write(ledc
, LP55231_PROG_PAGE_REG
, &page_num
, 1);
232 ledc_write(ledc
, LP55231_PROG_BASE_REG
+ page_offs
,
233 program
, segment_size
);
235 count
-= segment_size
;
236 program
+= segment_size
;
242 static void ledc_write_engctrl2(TiLp55231
*ledc
, uint8_t value
)
244 ledc_write(ledc
, LP55231_ENGCTRL2_REG
, &value
, 1);
248 /* Run an lp55231 program on a controller. */
249 static void ledc_run_program(TiLp55231
*ledc
,
250 const TiLp55231Program
*program_desc
)
255 /* All engines on hold. */
256 data
= LP55231_ENGCTRL1_CHIP_EN
;
257 ledc_write(ledc
, LP55231_ENGCTRL1_REG
, &data
, 1);
259 ledc_write_engctrl2(ledc
, LP55231_ENGCTRL2_ALL_DISABLE
);
260 ledc_write_engctrl2(ledc
, LP55231_ENGCTRL2_ALL_LOAD
);
262 ledc_write_program(ledc
, program_desc
->load_addr
,
263 program_desc
->program_text
,
264 program_desc
->program_size
);
266 for (i
= 0; i
< sizeof(program_desc
->engine_start_addr
); i
++)
267 ledc_write(ledc
, LP55231_ENG1_PROG_START
+ i
,
268 program_desc
->engine_start_addr
+ i
, 1);
270 data
= LP55231_ENGCTRL1_CHIP_EN
| LP55231_ENGCTRL1_ALL_ENG_GO
;
271 ledc_write(ledc
, LP55231_ENGCTRL1_REG
, &data
, 1);
272 ledc_write_engctrl2(ledc
, LP55231_ENGCTRL2_ALL_RUN
);
276 * Initialize a controller to a state were it is ready to accept programs, and
277 * try to confirm that we are in fact talking to a lp55231
279 static int ledc_init_validate(TiLp55231
*ledc
)
284 if (ledc_reset(ledc
))
287 /* Enable the chip, keep engines in hold state. */
288 data
= LP55231_ENGCTRL1_CHIP_EN
;
289 ledc_write(ledc
, LP55231_ENGCTRL1_REG
, &data
, 1);
292 * Internal clock, 3.3V output (pump 1x), autoincrement on multibyte
295 data
= LP55231_MISC_AUTOINCR
|
296 LP55231_MISC_PUMP_1X
| LP55231_MISC_INT_CLK
;
297 ledc_write(ledc
, LP55231_MISC_REG
, &data
, 1);
300 * All nine current control registers are supposed to return the same
303 for (i
= 0; i
< 9; i
++) {
304 ledc_read(ledc
, LP55231_D1_CRT_CTRL_REG
+ i
, &data
);
305 if (data
!= LP55231_CRT_CTRL_DEFAULT
) {
307 "%s: read %#2.2x from register %#x\n", __func__
,
308 data
, LP55231_D1_CRT_CTRL_REG
+ i
);
314 * Signal Depthcharge that the controller has been initiazed by
317 data
= LP55231_VARIABLE_COOKIE
;
318 ledc_write(ledc
, LP55231_VARIABLE_REG
, &data
, 1);
324 * Find a program matching screen type, and run it on both controllers, if
327 int ww_ring_display_pattern(unsigned int i2c_bus
, enum display_pattern pattern
)
330 const WwRingStateProg
*wwr_prog
;
333 ww_ring_init(i2c_bus
);
337 /* Last entry does not have any actual programs defined. */
338 for (wwr_prog
= wwr_state_programs
; wwr_prog
->programs
[0]; wwr_prog
++)
339 if (wwr_prog
->led_pattern
== pattern
) {
343 * First stop all running programs to avoid
344 * inerference between the controllers.
346 for (j
= 0; j
< WW_RING_NUM_LED_CONTROLLERS
; j
++) {
347 if (!lp55231s
[j
].dev_addr
)
351 LP55231_ENGCTRL2_ALL_DISABLE
);
354 for (j
= 0; j
< WW_RING_NUM_LED_CONTROLLERS
; j
++) {
355 if (!lp55231s
[j
].dev_addr
)
357 ledc_run_program(lp55231s
+ j
,
358 wwr_prog
->programs
[j
]);
363 printk(BIOS_WARNING
, "%s: did not find program for pattern %d\n",
370 #define LP55231_I2C_BASE_ADDR 0x32
372 static void ww_ring_init(unsigned int i2c_bus
)
377 for (i
= 0, ledc
= lp55231s
;
378 i
< WW_RING_NUM_LED_CONTROLLERS
;
381 ledc
->i2c_bus
= i2c_bus
;
382 ledc
->dev_addr
= LP55231_I2C_BASE_ADDR
+ i
;
384 if (!ledc_init_validate(ledc
))
387 ledc
->dev_addr
= 0; /* Mark disabled. */
390 printk(BIOS_INFO
, "WW_RING: initialized %d out of %d\n", count
, i
);
394 "WW_RING: will keep going anyway\n");
397 "WW_RING: LED ring not present\n");