1 /* SPDX-License-Identifier: GPL-2.0-only */
4 * This is a driver for the Whirlwind LED ring, which is equipped with two LED
5 * microcontrollers TI LP55231 (http://www.ti.com/product/lp55231), each of
6 * them driving three multicolor LEDs.
8 * The only connection between the ring and the main board is an i2c bus.
10 * This driver imitates a depthcharge display device. On initialization the
11 * driver sets up the controllers to prepare them to accept programs to run.
13 * When a certain vboot state needs to be indicated, the program for that
14 * state is loaded into the controllers, resulting in the state appropriate
18 #include <console/console.h>
20 #include <device/i2c_simple.h>
23 #include "drivers/i2c/ww_ring/ww_ring_programs.h"
25 /* I2c address of the first of the controllers, the rest are contiguous. */
26 #define WW_RING_BASE_ADDR 0x32
28 /* Key lp55231 registers. */
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
39 /* LP55231_D1_CRT_CTRL_REG, default value, applies to all nine of them */
40 #define LP55231_CRT_CTRL_DEFAULT 0xaf
42 /* LP55231_ENGCTRL1_REG fields */
43 #define LP55231_ENGCTRL1_CHIP_EN 0x40
44 #define LP55231_ENGCTRL1_ALL_ENG_GO 0x2a
46 /* LP55231_ENGCTRL2_REG fields. */
47 #define LP55231_ENGCTRL2_ALL_DISABLE 0
48 #define LP55231_ENGCTRL2_ALL_LOAD 0x15
49 #define LP55231_ENGCTRL2_ALL_RUN 0x2a
51 /* LP55231_MISC_REG fields. */
52 #define LP55231_MISC_AUTOINCR (1 << 6)
53 #define LP55231_MISC_PUMP_1X (1 << 3)
54 #define LP55231_MISC_INT_CLK (3 << 0)
57 * LP55231_VARIABLE_REG cookie value. It indicates to depthcharge that the
58 * ring has been initialized by coreboot.
60 #define LP55231_VARIABLE_COOKIE 0xb4
62 /* Goes into LP55231_RESET_REG to reset the chip. */
63 #define LP55231_RESET_VALUE 0xff
66 * The controller has 192 bytes of SRAM for code/data, available as six 32 byte
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)
74 * Structure to cache data relevant to accessing one controller. I2c interface
75 * to use, device address on the i2c bus and a data buffer for write
76 * transactions. The most bytes sent at a time is the register address plus
77 * the program page size.
82 uint8_t data_buffer
[LP55231_PROG_PAGE_SIZE
+ 1];
85 static void ww_ring_init(unsigned int i2c_bus
);
87 /* Controller descriptors. */
88 static TiLp55231 lp55231s
[WW_RING_NUM_LED_CONTROLLERS
];
91 * i2c transfer function for the driver. To keep things simple, the function
92 * repeats the transfer, if the first attempt fails. This is OK with the
93 * controller and makes it easier to handle errors.
95 * Note that the reset register accesses are expected to fail on writes, but
96 * due to a bug in the ipq806x i2c controller, the error is reported on the
97 * following read attempt.
99 * To work around this the driver writes and then reads the reset register,
100 * the transfer function ignores errors when accessing the reset register.
103 static int ledc_transfer(TiLp55231
*ledc
, struct i2c_msg
*segs
,
104 int seg_count
, int reset
)
106 int rv
, max_attempts
= 2;
108 while (max_attempts
--) {
109 rv
= i2c_transfer(ledc
->i2c_bus
, segs
, seg_count
);
111 /* Accessing reset register is expected to fail. */
119 "%s: dev %#x, reg %#x, %s transaction error.\n",
120 __func__
, segs
->slave
, segs
->buf
[0],
121 seg_count
== 1 ? "write" : "read");
130 * The controller is programmed to autoincrement on writes, so up to page size
131 * bytes can be transmitted in one write transaction.
133 static int ledc_write(TiLp55231
*ledc
, uint8_t start_addr
,
134 const uint8_t *data
, unsigned int count
)
138 if (count
> (sizeof(ledc
->data_buffer
) - 1)) {
139 printk(BIOS_WARNING
, "%s: transfer size too large (%d bytes)\n",
144 memcpy(ledc
->data_buffer
+ 1, data
, count
);
145 ledc
->data_buffer
[0] = start_addr
;
148 seg
.slave
= ledc
->dev_addr
;
149 seg
.buf
= ledc
->data_buffer
;
152 return ledc_transfer(ledc
, &seg
, 1, start_addr
== LP55231_RESET_REG
);
155 /* To keep things simple, read is limited to one byte at a time. */
156 static int ledc_read(TiLp55231
*ledc
, uint8_t addr
, uint8_t *data
)
158 struct i2c_msg seg
[2];
161 seg
[0].slave
= ledc
->dev_addr
;
165 seg
[1].flags
= I2C_M_RD
;
166 seg
[1].slave
= ledc
->dev_addr
;
170 return ledc_transfer(ledc
, seg
, ARRAY_SIZE(seg
),
171 addr
== LP55231_RESET_REG
);
175 * Reset transaction is expected to result in a failing i2c command. But even
176 * before trying it, read the reset register, which is supposed to always
177 * return 0. If this fails - there is no lp55231 at this address.
179 * Return 0 on success, -1 on failure to detect controller.
181 static int ledc_reset(TiLp55231
*ledc
)
186 ledc_read(ledc
, LP55231_RESET_REG
, &data
);
189 "WW_RING: no controller found at address %#2.2x\n",
194 data
= LP55231_RESET_VALUE
;
195 ledc_write(ledc
, LP55231_RESET_REG
, &data
, 1);
198 * This read is not necessary for the chip reset, but is required to
199 * work around the i2c driver bug where the missing ACK on the last
200 * byte of the write transaction is ignored, but the next transaction
203 ledc_read(ledc
, LP55231_RESET_REG
, &data
);
208 * Write a program into the internal lp55231 memory. Split write transactions
209 * into sections fitting into memory pages.
211 static void ledc_write_program(TiLp55231
*ledc
, uint8_t load_addr
,
212 const uint8_t *program
, unsigned int count
)
214 uint8_t page_num
= load_addr
/ LP55231_PROG_PAGE_SIZE
;
215 unsigned int page_offs
= load_addr
% LP55231_PROG_PAGE_SIZE
;
217 if ((load_addr
+ count
) > LP55231_MAX_PROG_SIZE
) {
219 "%s: program of size %#x does not fit at addr %#x\n",
220 __func__
, count
, load_addr
);
225 unsigned int segment_size
= LP55231_PROG_PAGE_SIZE
- page_offs
;
227 if (segment_size
> count
)
228 segment_size
= count
;
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
);
234 count
-= segment_size
;
235 program
+= segment_size
;
241 static void ledc_write_engctrl2(TiLp55231
*ledc
, uint8_t value
)
243 ledc_write(ledc
, LP55231_ENGCTRL2_REG
, &value
, 1);
247 /* Run an lp55231 program on a controller. */
248 static void ledc_run_program(TiLp55231
*ledc
,
249 const TiLp55231Program
*program_desc
)
254 /* All engines on hold. */
255 data
= LP55231_ENGCTRL1_CHIP_EN
;
256 ledc_write(ledc
, LP55231_ENGCTRL1_REG
, &data
, 1);
258 ledc_write_engctrl2(ledc
, LP55231_ENGCTRL2_ALL_DISABLE
);
259 ledc_write_engctrl2(ledc
, LP55231_ENGCTRL2_ALL_LOAD
);
261 ledc_write_program(ledc
, program_desc
->load_addr
,
262 program_desc
->program_text
,
263 program_desc
->program_size
);
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);
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
);
275 * Initialize a controller to a state were it is ready to accept programs, and
276 * try to confirm that we are in fact talking to a lp55231
278 static int ledc_init_validate(TiLp55231
*ledc
)
283 if (ledc_reset(ledc
))
286 /* Enable the chip, keep engines in hold state. */
287 data
= LP55231_ENGCTRL1_CHIP_EN
;
288 ledc_write(ledc
, LP55231_ENGCTRL1_REG
, &data
, 1);
291 * Internal clock, 3.3V output (pump 1x), autoincrement on multibyte
294 data
= LP55231_MISC_AUTOINCR
|
295 LP55231_MISC_PUMP_1X
| LP55231_MISC_INT_CLK
;
296 ledc_write(ledc
, LP55231_MISC_REG
, &data
, 1);
299 * All nine current control registers are supposed to return the same
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
) {
306 "%s: read %#2.2x from register %#x\n", __func__
,
307 data
, LP55231_D1_CRT_CTRL_REG
+ i
);
313 * Signal Depthcharge that the controller has been initialized by
316 data
= LP55231_VARIABLE_COOKIE
;
317 ledc_write(ledc
, LP55231_VARIABLE_REG
, &data
, 1);
323 * Find a program matching screen type, and run it on both controllers, if
326 int ww_ring_display_pattern(unsigned int i2c_bus
, enum display_pattern pattern
)
329 const WwRingStateProg
*wwr_prog
;
332 ww_ring_init(i2c_bus
);
336 /* Last entry does not have any actual programs defined. */
337 for (wwr_prog
= wwr_state_programs
; wwr_prog
->programs
[0]; wwr_prog
++)
338 if (wwr_prog
->led_pattern
== pattern
) {
342 * First stop all running programs to avoid
343 * interference between the controllers.
345 for (j
= 0; j
< WW_RING_NUM_LED_CONTROLLERS
; j
++) {
346 if (!lp55231s
[j
].dev_addr
)
350 LP55231_ENGCTRL2_ALL_DISABLE
);
353 for (j
= 0; j
< WW_RING_NUM_LED_CONTROLLERS
; j
++) {
354 if (!lp55231s
[j
].dev_addr
)
356 ledc_run_program(lp55231s
+ j
,
357 wwr_prog
->programs
[j
]);
362 printk(BIOS_WARNING
, "%s: did not find program for pattern %d\n",
368 #define LP55231_I2C_BASE_ADDR 0x32
370 static void ww_ring_init(unsigned int i2c_bus
)
375 for (i
= 0, ledc
= lp55231s
;
376 i
< WW_RING_NUM_LED_CONTROLLERS
;
378 ledc
->i2c_bus
= i2c_bus
;
379 ledc
->dev_addr
= LP55231_I2C_BASE_ADDR
+ i
;
381 if (!ledc_init_validate(ledc
))
384 ledc
->dev_addr
= 0; /* Mark disabled. */
387 printk(BIOS_INFO
, "WW_RING: initialized %d out of %d\n", count
, i
);
391 "WW_RING: will keep going anyway\n");
394 "WW_RING: LED ring not present\n");