1 // SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
2 /*
3 * UCSI driver for STMicroelectronics STM32G0 Type-C PD controller
4 *
5 * Copyright (C) 2022, STMicroelectronics - All Rights Reserved
6 * Author: Fabrice Gasnier <fabrice.gasnier@foss.st.com>.
7 */
8
9 #include <linux/delay.h>
10 #include <linux/firmware.h>
11 #include <linux/i2c.h>
12 #include <linux/interrupt.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/unaligned.h>
16
17 #include "ucsi.h"
18
19 /* STM32G0 I2C bootloader addr: 0b1010001x (See AN2606) */
20 #define STM32G0_I2C_BL_ADDR (0xa2 >> 1)
21
22 /* STM32G0 I2C bootloader max data size */
23 #define STM32G0_I2C_BL_SZ 256
24
25 /* STM32 I2C bootloader commands (See AN4221) */
26 #define STM32_CMD_GVR 0x01 /* Gets the bootloader version */
27 #define STM32_CMD_GVR_LEN 1
28 #define STM32_CMD_RM 0x11 /* Reag memory */
29 #define STM32_CMD_WM 0x31 /* Write memory */
30 #define STM32_CMD_ADDR_LEN 5 /* Address len for go, mem write... */
31 #define STM32_CMD_ERASE 0x44 /* Erase page, bank or all */
32 #define STM32_CMD_ERASE_SPECIAL_LEN 3
33 #define STM32_CMD_GLOBAL_MASS_ERASE 0xffff /* All-bank erase */
34
35 /* STM32 I2C bootloader answer status */
36 #define STM32G0_I2C_BL_ACK 0x79
37 #define STM32G0_I2C_BL_NACK 0x1f
38 #define STM32G0_I2C_BL_BUSY 0x76
39
40 /* STM32G0 flash definitions */
41 #define STM32G0_USER_OPTION_BYTES 0x1fff7800
42 #define STM32G0_USER_OB_NBOOT0 BIT(26)
43 #define STM32G0_USER_OB_NBOOT_SEL BIT(24)
44 #define STM32G0_USER_OB_BOOT_MAIN (STM32G0_USER_OB_NBOOT0 | STM32G0_USER_OB_NBOOT_SEL)
45 #define STM32G0_MAIN_MEM_ADDR 0x08000000
46
47 /* STM32 Firmware definitions: additional commands */
48 #define STM32G0_FW_GETVER 0x00 /* Gets the firmware version */
49 #define STM32G0_FW_GETVER_LEN 4
50 #define STM32G0_FW_RSTGOBL 0x21 /* Reset and go to bootloader */
51 #define STM32G0_FW_KEYWORD 0xa56959a6
52
53 /* ucsi_stm32g0_fw_info located at the end of the firmware */
54 struct ucsi_stm32g0_fw_info {
55 u32 version;
56 u32 keyword;
57 };
58
59 struct ucsi_stm32g0 {
60 struct i2c_client *client;
61 struct i2c_client *i2c_bl;
62 bool in_bootloader;
63 u8 bl_version;
64 struct device *dev;
65 const char *fw_name;
66 struct ucsi *ucsi;
67 bool suspended;
68 bool wakeup_event;
69 };
70
71 /*
72 * Bootloader commands helpers:
73 * - send command (2 bytes)
74 * - check ack
75 * Then either:
76 * - receive data
77 * - receive data + check ack
78 * - send data + check ack
79 * These operations depends on the command and have various length.
80 */
ucsi_stm32g0_bl_check_ack(struct ucsi * ucsi)81 static int ucsi_stm32g0_bl_check_ack(struct ucsi *ucsi)
82 {
83 struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi);
84 struct i2c_client *client = g0->i2c_bl;
85 unsigned char ack;
86 struct i2c_msg msg[] = {
87 {
88 .addr = client->addr,
89 .flags = I2C_M_RD,
90 .len = 1,
91 .buf = &ack,
92 },
93 };
94 int ret;
95
96 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
97 if (ret != ARRAY_SIZE(msg)) {
98 dev_err(g0->dev, "i2c bl ack (%02x), error: %d\n", client->addr, ret);
99
100 return ret < 0 ? ret : -EIO;
101 }
102
103 /* The 'ack' byte should contain bootloader answer: ack/nack/busy */
104 switch (ack) {
105 case STM32G0_I2C_BL_ACK:
106 return 0;
107 case STM32G0_I2C_BL_NACK:
108 return -ENOENT;
109 case STM32G0_I2C_BL_BUSY:
110 return -EBUSY;
111 default:
112 dev_err(g0->dev, "i2c bl ack (%02x), invalid byte: %02x\n",
113 client->addr, ack);
114 return -EINVAL;
115 }
116 }
117
ucsi_stm32g0_bl_cmd_check_ack(struct ucsi * ucsi,unsigned int cmd,bool check_ack)118 static int ucsi_stm32g0_bl_cmd_check_ack(struct ucsi *ucsi, unsigned int cmd, bool check_ack)
119 {
120 struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi);
121 struct i2c_client *client = g0->i2c_bl;
122 unsigned char buf[2];
123 struct i2c_msg msg[] = {
124 {
125 .addr = client->addr,
126 .flags = 0,
127 .len = sizeof(buf),
128 .buf = buf,
129 },
130 };
131 int ret;
132
133 /*
134 * Send STM32 bootloader command format is two bytes:
135 * - command code
136 * - XOR'ed command code
137 */
138 buf[0] = cmd;
139 buf[1] = cmd ^ 0xff;
140
141 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
142 if (ret != ARRAY_SIZE(msg)) {
143 dev_dbg(g0->dev, "i2c bl cmd %d (%02x), error: %d\n", cmd, client->addr, ret);
144
145 return ret < 0 ? ret : -EIO;
146 }
147
148 if (check_ack)
149 return ucsi_stm32g0_bl_check_ack(ucsi);
150
151 return 0;
152 }
153
ucsi_stm32g0_bl_cmd(struct ucsi * ucsi,unsigned int cmd)154 static int ucsi_stm32g0_bl_cmd(struct ucsi *ucsi, unsigned int cmd)
155 {
156 return ucsi_stm32g0_bl_cmd_check_ack(ucsi, cmd, true);
157 }
158
ucsi_stm32g0_bl_rcv_check_ack(struct ucsi * ucsi,void * data,size_t len,bool check_ack)159 static int ucsi_stm32g0_bl_rcv_check_ack(struct ucsi *ucsi, void *data, size_t len, bool check_ack)
160 {
161 struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi);
162 struct i2c_client *client = g0->i2c_bl;
163 struct i2c_msg msg[] = {
164 {
165 .addr = client->addr,
166 .flags = I2C_M_RD,
167 .len = len,
168 .buf = data,
169 },
170 };
171 int ret;
172
173 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
174 if (ret != ARRAY_SIZE(msg)) {
175 dev_err(g0->dev, "i2c bl rcv %02x, error: %d\n", client->addr, ret);
176
177 return ret < 0 ? ret : -EIO;
178 }
179
180 if (check_ack)
181 return ucsi_stm32g0_bl_check_ack(ucsi);
182
183 return 0;
184 }
185
ucsi_stm32g0_bl_rcv(struct ucsi * ucsi,void * data,size_t len)186 static int ucsi_stm32g0_bl_rcv(struct ucsi *ucsi, void *data, size_t len)
187 {
188 return ucsi_stm32g0_bl_rcv_check_ack(ucsi, data, len, true);
189 }
190
ucsi_stm32g0_bl_rcv_woack(struct ucsi * ucsi,void * data,size_t len)191 static int ucsi_stm32g0_bl_rcv_woack(struct ucsi *ucsi, void *data, size_t len)
192 {
193 return ucsi_stm32g0_bl_rcv_check_ack(ucsi, data, len, false);
194 }
195
ucsi_stm32g0_bl_send(struct ucsi * ucsi,void * data,size_t len)196 static int ucsi_stm32g0_bl_send(struct ucsi *ucsi, void *data, size_t len)
197 {
198 struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi);
199 struct i2c_client *client = g0->i2c_bl;
200 struct i2c_msg msg[] = {
201 {
202 .addr = client->addr,
203 .flags = 0,
204 .len = len,
205 .buf = data,
206 },
207 };
208 int ret;
209
210 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
211 if (ret != ARRAY_SIZE(msg)) {
212 dev_err(g0->dev, "i2c bl send %02x, error: %d\n", client->addr, ret);
213
214 return ret < 0 ? ret : -EIO;
215 }
216
217 return ucsi_stm32g0_bl_check_ack(ucsi);
218 }
219
220 /* Bootloader commands */
ucsi_stm32g0_bl_get_version(struct ucsi * ucsi,u8 * bl_version)221 static int ucsi_stm32g0_bl_get_version(struct ucsi *ucsi, u8 *bl_version)
222 {
223 int ret;
224
225 ret = ucsi_stm32g0_bl_cmd(ucsi, STM32_CMD_GVR);
226 if (ret)
227 return ret;
228
229 return ucsi_stm32g0_bl_rcv(ucsi, bl_version, STM32_CMD_GVR_LEN);
230 }
231
ucsi_stm32g0_bl_send_addr(struct ucsi * ucsi,u32 addr)232 static int ucsi_stm32g0_bl_send_addr(struct ucsi *ucsi, u32 addr)
233 {
234 u8 data8[STM32_CMD_ADDR_LEN];
235
236 /* Address format: 4 bytes addr (MSB first) + XOR'ed addr bytes */
237 put_unaligned_be32(addr, data8);
238 data8[4] = data8[0] ^ data8[1] ^ data8[2] ^ data8[3];
239
240 return ucsi_stm32g0_bl_send(ucsi, data8, STM32_CMD_ADDR_LEN);
241 }
242
ucsi_stm32g0_bl_global_mass_erase(struct ucsi * ucsi)243 static int ucsi_stm32g0_bl_global_mass_erase(struct ucsi *ucsi)
244 {
245 u8 data8[4];
246 u16 *data16 = (u16 *)&data8[0];
247 int ret;
248
249 data16[0] = STM32_CMD_GLOBAL_MASS_ERASE;
250 data8[2] = data8[0] ^ data8[1];
251
252 ret = ucsi_stm32g0_bl_cmd(ucsi, STM32_CMD_ERASE);
253 if (ret)
254 return ret;
255
256 return ucsi_stm32g0_bl_send(ucsi, data8, STM32_CMD_ERASE_SPECIAL_LEN);
257 }
258
ucsi_stm32g0_bl_write(struct ucsi * ucsi,u32 addr,const void * data,size_t len)259 static int ucsi_stm32g0_bl_write(struct ucsi *ucsi, u32 addr, const void *data, size_t len)
260 {
261 u8 *data8;
262 int i, ret;
263
264 if (!len || len > STM32G0_I2C_BL_SZ)
265 return -EINVAL;
266
267 /* Write memory: len bytes -1, data up to 256 bytes + XOR'ed bytes */
268 data8 = kmalloc(STM32G0_I2C_BL_SZ + 2, GFP_KERNEL);
269 if (!data8)
270 return -ENOMEM;
271
272 ret = ucsi_stm32g0_bl_cmd(ucsi, STM32_CMD_WM);
273 if (ret)
274 goto free;
275
276 ret = ucsi_stm32g0_bl_send_addr(ucsi, addr);
277 if (ret)
278 goto free;
279
280 data8[0] = len - 1;
281 memcpy(data8 + 1, data, len);
282 data8[len + 1] = data8[0];
283 for (i = 1; i <= len; i++)
284 data8[len + 1] ^= data8[i];
285
286 ret = ucsi_stm32g0_bl_send(ucsi, data8, len + 2);
287 free:
288 kfree(data8);
289
290 return ret;
291 }
292
ucsi_stm32g0_bl_read(struct ucsi * ucsi,u32 addr,void * data,size_t len)293 static int ucsi_stm32g0_bl_read(struct ucsi *ucsi, u32 addr, void *data, size_t len)
294 {
295 int ret;
296
297 if (!len || len > STM32G0_I2C_BL_SZ)
298 return -EINVAL;
299
300 ret = ucsi_stm32g0_bl_cmd(ucsi, STM32_CMD_RM);
301 if (ret)
302 return ret;
303
304 ret = ucsi_stm32g0_bl_send_addr(ucsi, addr);
305 if (ret)
306 return ret;
307
308 ret = ucsi_stm32g0_bl_cmd(ucsi, len - 1);
309 if (ret)
310 return ret;
311
312 return ucsi_stm32g0_bl_rcv_woack(ucsi, data, len);
313 }
314
315 /* Firmware commands (the same address as the bootloader) */
ucsi_stm32g0_fw_cmd(struct ucsi * ucsi,unsigned int cmd)316 static int ucsi_stm32g0_fw_cmd(struct ucsi *ucsi, unsigned int cmd)
317 {
318 return ucsi_stm32g0_bl_cmd_check_ack(ucsi, cmd, false);
319 }
320
ucsi_stm32g0_fw_rcv(struct ucsi * ucsi,void * data,size_t len)321 static int ucsi_stm32g0_fw_rcv(struct ucsi *ucsi, void *data, size_t len)
322 {
323 return ucsi_stm32g0_bl_rcv_woack(ucsi, data, len);
324 }
325
326 /* UCSI ops */
ucsi_stm32g0_read(struct ucsi * ucsi,unsigned int offset,void * val,size_t len)327 static int ucsi_stm32g0_read(struct ucsi *ucsi, unsigned int offset, void *val, size_t len)
328 {
329 struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi);
330 struct i2c_client *client = g0->client;
331 u8 reg = offset;
332 struct i2c_msg msg[] = {
333 {
334 .addr = client->addr,
335 .flags = 0,
336 .len = 1,
337 .buf = ®,
338 },
339 {
340 .addr = client->addr,
341 .flags = I2C_M_RD,
342 .len = len,
343 .buf = val,
344 },
345 };
346 int ret;
347
348 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
349 if (ret != ARRAY_SIZE(msg)) {
350 dev_err(g0->dev, "i2c read %02x, %02x error: %d\n", client->addr, reg, ret);
351
352 return ret < 0 ? ret : -EIO;
353 }
354
355 return 0;
356 }
357
ucsi_stm32g0_read_version(struct ucsi * ucsi,u16 * version)358 static int ucsi_stm32g0_read_version(struct ucsi *ucsi, u16 *version)
359 {
360 return ucsi_stm32g0_read(ucsi, UCSI_VERSION, version, sizeof(*version));
361 }
362
ucsi_stm32g0_read_cci(struct ucsi * ucsi,u32 * cci)363 static int ucsi_stm32g0_read_cci(struct ucsi *ucsi, u32 *cci)
364 {
365 return ucsi_stm32g0_read(ucsi, UCSI_CCI, cci, sizeof(*cci));
366 }
367
ucsi_stm32g0_read_message_in(struct ucsi * ucsi,void * val,size_t len)368 static int ucsi_stm32g0_read_message_in(struct ucsi *ucsi, void *val, size_t len)
369 {
370 return ucsi_stm32g0_read(ucsi, UCSI_MESSAGE_IN, val, len);
371 }
372
ucsi_stm32g0_async_control(struct ucsi * ucsi,u64 command)373 static int ucsi_stm32g0_async_control(struct ucsi *ucsi, u64 command)
374 {
375 struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi);
376 struct i2c_client *client = g0->client;
377 struct i2c_msg msg[] = {
378 {
379 .addr = client->addr,
380 .flags = 0,
381 }
382 };
383 unsigned char *buf;
384 int ret;
385
386 buf = kmalloc(sizeof(command) + 1, GFP_KERNEL);
387 if (!buf)
388 return -ENOMEM;
389
390 buf[0] = UCSI_CONTROL;
391 memcpy(&buf[1], &command, sizeof(command));
392 msg[0].len = sizeof(command) + 1;
393 msg[0].buf = buf;
394
395 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
396 kfree(buf);
397 if (ret != ARRAY_SIZE(msg)) {
398 dev_err(g0->dev, "i2c write %02x, %02x error: %d\n", client->addr, UCSI_CONTROL, ret);
399
400 return ret < 0 ? ret : -EIO;
401 }
402
403 return 0;
404 }
405
ucsi_stm32g0_irq_handler(int irq,void * data)406 static irqreturn_t ucsi_stm32g0_irq_handler(int irq, void *data)
407 {
408 struct ucsi_stm32g0 *g0 = data;
409 u32 cci;
410 int ret;
411
412 if (g0->suspended)
413 g0->wakeup_event = true;
414
415 ret = ucsi_stm32g0_read(g0->ucsi, UCSI_CCI, &cci, sizeof(cci));
416 if (ret)
417 return IRQ_NONE;
418
419 ucsi_notify_common(g0->ucsi, cci);
420
421 return IRQ_HANDLED;
422 }
423
424 static const struct ucsi_operations ucsi_stm32g0_ops = {
425 .read_version = ucsi_stm32g0_read_version,
426 .read_cci = ucsi_stm32g0_read_cci,
427 .poll_cci = ucsi_stm32g0_read_cci,
428 .read_message_in = ucsi_stm32g0_read_message_in,
429 .sync_control = ucsi_sync_control_common,
430 .async_control = ucsi_stm32g0_async_control,
431 };
432
ucsi_stm32g0_register(struct ucsi * ucsi)433 static int ucsi_stm32g0_register(struct ucsi *ucsi)
434 {
435 struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi);
436 struct i2c_client *client = g0->client;
437 int ret;
438
439 /* Request alert interrupt */
440 ret = request_threaded_irq(client->irq, NULL, ucsi_stm32g0_irq_handler, IRQF_ONESHOT,
441 dev_name(g0->dev), g0);
442 if (ret) {
443 dev_err(g0->dev, "request IRQ failed: %d\n", ret);
444 return ret;
445 }
446
447 ret = ucsi_register(ucsi);
448 if (ret) {
449 dev_err_probe(g0->dev, ret, "ucsi_register failed\n");
450 free_irq(client->irq, g0);
451 return ret;
452 }
453
454 return 0;
455 }
456
ucsi_stm32g0_unregister(struct ucsi * ucsi)457 static void ucsi_stm32g0_unregister(struct ucsi *ucsi)
458 {
459 struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi);
460 struct i2c_client *client = g0->client;
461
462 ucsi_unregister(ucsi);
463 free_irq(client->irq, g0);
464 }
465
ucsi_stm32g0_fw_cb(const struct firmware * fw,void * context)466 static void ucsi_stm32g0_fw_cb(const struct firmware *fw, void *context)
467 {
468 struct ucsi_stm32g0 *g0;
469 const u8 *data, *end;
470 const struct ucsi_stm32g0_fw_info *fw_info;
471 u32 addr = STM32G0_MAIN_MEM_ADDR, ob, fw_version;
472 int ret, size;
473
474 if (!context)
475 return;
476
477 g0 = ucsi_get_drvdata(context);
478
479 if (!fw)
480 goto fw_release;
481
482 fw_info = (struct ucsi_stm32g0_fw_info *)(fw->data + fw->size - sizeof(*fw_info));
483
484 if (!g0->in_bootloader) {
485 /* Read running firmware version */
486 ret = ucsi_stm32g0_fw_cmd(g0->ucsi, STM32G0_FW_GETVER);
487 if (ret) {
488 dev_err(g0->dev, "Get version cmd failed %d\n", ret);
489 goto fw_release;
490 }
491 ret = ucsi_stm32g0_fw_rcv(g0->ucsi, &fw_version,
492 STM32G0_FW_GETVER_LEN);
493 if (ret) {
494 dev_err(g0->dev, "Get version failed %d\n", ret);
495 goto fw_release;
496 }
497
498 /* Sanity check on keyword and firmware version */
499 if (fw_info->keyword != STM32G0_FW_KEYWORD || fw_info->version == fw_version)
500 goto fw_release;
501
502 dev_info(g0->dev, "Flashing FW: %08x (%08x cur)\n", fw_info->version, fw_version);
503
504 /* Switch to bootloader mode */
505 ucsi_stm32g0_unregister(g0->ucsi);
506 ret = ucsi_stm32g0_fw_cmd(g0->ucsi, STM32G0_FW_RSTGOBL);
507 if (ret) {
508 dev_err(g0->dev, "bootloader cmd failed %d\n", ret);
509 goto fw_release;
510 }
511 g0->in_bootloader = true;
512
513 /* STM32G0 reboot delay */
514 msleep(100);
515 }
516
517 ret = ucsi_stm32g0_bl_global_mass_erase(g0->ucsi);
518 if (ret) {
519 dev_err(g0->dev, "Erase failed %d\n", ret);
520 goto fw_release;
521 }
522
523 data = fw->data;
524 end = fw->data + fw->size;
525 while (data < end) {
526 if ((end - data) < STM32G0_I2C_BL_SZ)
527 size = end - data;
528 else
529 size = STM32G0_I2C_BL_SZ;
530
531 ret = ucsi_stm32g0_bl_write(g0->ucsi, addr, data, size);
532 if (ret) {
533 dev_err(g0->dev, "Write failed %d\n", ret);
534 goto fw_release;
535 }
536 addr += size;
537 data += size;
538 }
539
540 dev_dbg(g0->dev, "Configure to boot from main flash\n");
541
542 ret = ucsi_stm32g0_bl_read(g0->ucsi, STM32G0_USER_OPTION_BYTES, &ob, sizeof(ob));
543 if (ret) {
544 dev_err(g0->dev, "read user option bytes failed %d\n", ret);
545 goto fw_release;
546 }
547
548 dev_dbg(g0->dev, "STM32G0_USER_OPTION_BYTES 0x%08x\n", ob);
549
550 /* Configure user option bytes to boot from main flash next time */
551 ob |= STM32G0_USER_OB_BOOT_MAIN;
552
553 /* Writing option bytes will also reset G0 for updates to be loaded */
554 ret = ucsi_stm32g0_bl_write(g0->ucsi, STM32G0_USER_OPTION_BYTES, &ob, sizeof(ob));
555 if (ret) {
556 dev_err(g0->dev, "write user option bytes failed %d\n", ret);
557 goto fw_release;
558 }
559
560 dev_info(g0->dev, "Starting, option bytes:0x%08x\n", ob);
561
562 /* STM32G0 FW boot delay */
563 msleep(500);
564
565 /* Register UCSI interface */
566 if (!ucsi_stm32g0_register(g0->ucsi))
567 g0->in_bootloader = false;
568
569 fw_release:
570 release_firmware(fw);
571 }
572
ucsi_stm32g0_probe_bootloader(struct ucsi * ucsi)573 static int ucsi_stm32g0_probe_bootloader(struct ucsi *ucsi)
574 {
575 struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi);
576 int ret;
577 u16 ucsi_version;
578
579 /* firmware-name is optional */
580 if (device_property_present(g0->dev, "firmware-name")) {
581 ret = device_property_read_string(g0->dev, "firmware-name", &g0->fw_name);
582 if (ret < 0)
583 return dev_err_probe(g0->dev, ret, "Error reading firmware-name\n");
584 }
585
586 if (g0->fw_name) {
587 /* STM32G0 in bootloader mode communicates at reserved address 0x51 */
588 g0->i2c_bl = i2c_new_dummy_device(g0->client->adapter, STM32G0_I2C_BL_ADDR);
589 if (IS_ERR(g0->i2c_bl)) {
590 ret = dev_err_probe(g0->dev, PTR_ERR(g0->i2c_bl),
591 "Failed to register bootloader I2C address\n");
592 return ret;
593 }
594 }
595
596 /*
597 * Try to guess if the STM32G0 is running a UCSI firmware. First probe the UCSI FW at its
598 * i2c address. Fallback to bootloader i2c address only if firmware-name is specified.
599 */
600 ret = ucsi_stm32g0_read(ucsi, UCSI_VERSION, &ucsi_version, sizeof(ucsi_version));
601 if (!ret || !g0->fw_name)
602 return ret;
603
604 /* Speculatively read the bootloader version that has a known length. */
605 ret = ucsi_stm32g0_bl_get_version(ucsi, &g0->bl_version);
606 if (ret < 0) {
607 i2c_unregister_device(g0->i2c_bl);
608 return ret;
609 }
610
611 /* Device in bootloader mode */
612 g0->in_bootloader = true;
613 dev_info(g0->dev, "Bootloader Version 0x%02x\n", g0->bl_version);
614
615 return 0;
616 }
617
ucsi_stm32g0_probe(struct i2c_client * client)618 static int ucsi_stm32g0_probe(struct i2c_client *client)
619 {
620 struct device *dev = &client->dev;
621 struct ucsi_stm32g0 *g0;
622 int ret;
623
624 g0 = devm_kzalloc(dev, sizeof(*g0), GFP_KERNEL);
625 if (!g0)
626 return -ENOMEM;
627
628 g0->dev = dev;
629 g0->client = client;
630 i2c_set_clientdata(client, g0);
631
632 g0->ucsi = ucsi_create(dev, &ucsi_stm32g0_ops);
633 if (IS_ERR(g0->ucsi))
634 return PTR_ERR(g0->ucsi);
635
636 ucsi_set_drvdata(g0->ucsi, g0);
637
638 ret = ucsi_stm32g0_probe_bootloader(g0->ucsi);
639 if (ret < 0)
640 goto destroy;
641
642 /*
643 * Don't register in bootloader mode: wait for the firmware to be loaded and started before
644 * registering UCSI device.
645 */
646 if (!g0->in_bootloader) {
647 ret = ucsi_stm32g0_register(g0->ucsi);
648 if (ret < 0)
649 goto freei2c;
650 }
651
652 if (g0->fw_name) {
653 /*
654 * Asynchronously flash (e.g. bootloader mode) or update the running firmware,
655 * not to hang the boot process
656 */
657 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_UEVENT, g0->fw_name, g0->dev,
658 GFP_KERNEL, g0->ucsi, ucsi_stm32g0_fw_cb);
659 if (ret < 0) {
660 dev_err_probe(dev, ret, "firmware request failed\n");
661 goto unregister;
662 }
663 }
664
665 return 0;
666
667 unregister:
668 if (!g0->in_bootloader)
669 ucsi_stm32g0_unregister(g0->ucsi);
670 freei2c:
671 if (g0->fw_name)
672 i2c_unregister_device(g0->i2c_bl);
673 destroy:
674 ucsi_destroy(g0->ucsi);
675
676 return ret;
677 }
678
ucsi_stm32g0_remove(struct i2c_client * client)679 static void ucsi_stm32g0_remove(struct i2c_client *client)
680 {
681 struct ucsi_stm32g0 *g0 = i2c_get_clientdata(client);
682
683 if (!g0->in_bootloader)
684 ucsi_stm32g0_unregister(g0->ucsi);
685 if (g0->fw_name)
686 i2c_unregister_device(g0->i2c_bl);
687 ucsi_destroy(g0->ucsi);
688 }
689
ucsi_stm32g0_suspend(struct device * dev)690 static int ucsi_stm32g0_suspend(struct device *dev)
691 {
692 struct ucsi_stm32g0 *g0 = dev_get_drvdata(dev);
693 struct i2c_client *client = g0->client;
694
695 if (g0->in_bootloader)
696 return 0;
697
698 /* Keep the interrupt disabled until the i2c bus has been resumed */
699 disable_irq(client->irq);
700
701 g0->suspended = true;
702 g0->wakeup_event = false;
703
704 if (device_may_wakeup(dev) || device_wakeup_path(dev))
705 enable_irq_wake(client->irq);
706
707 return 0;
708 }
709
ucsi_stm32g0_resume(struct device * dev)710 static int ucsi_stm32g0_resume(struct device *dev)
711 {
712 struct ucsi_stm32g0 *g0 = dev_get_drvdata(dev);
713 struct i2c_client *client = g0->client;
714
715 if (g0->in_bootloader)
716 return 0;
717
718 if (device_may_wakeup(dev) || device_wakeup_path(dev))
719 disable_irq_wake(client->irq);
720
721 enable_irq(client->irq);
722
723 /* Enforce any pending handler gets called to signal a wakeup_event */
724 synchronize_irq(client->irq);
725
726 if (g0->wakeup_event)
727 pm_wakeup_event(g0->dev, 0);
728
729 g0->suspended = false;
730
731 return 0;
732 }
733
734 static DEFINE_SIMPLE_DEV_PM_OPS(ucsi_stm32g0_pm_ops, ucsi_stm32g0_suspend, ucsi_stm32g0_resume);
735
736 static const struct of_device_id __maybe_unused ucsi_stm32g0_typec_of_match[] = {
737 { .compatible = "st,stm32g0-typec" },
738 {},
739 };
740 MODULE_DEVICE_TABLE(of, ucsi_stm32g0_typec_of_match);
741
742 static const struct i2c_device_id ucsi_stm32g0_typec_i2c_devid[] = {
743 { "stm32g0-typec" },
744 {}
745 };
746 MODULE_DEVICE_TABLE(i2c, ucsi_stm32g0_typec_i2c_devid);
747
748 static struct i2c_driver ucsi_stm32g0_i2c_driver = {
749 .driver = {
750 .name = "ucsi-stm32g0-i2c",
751 .of_match_table = of_match_ptr(ucsi_stm32g0_typec_of_match),
752 .pm = pm_sleep_ptr(&ucsi_stm32g0_pm_ops),
753 },
754 .probe = ucsi_stm32g0_probe,
755 .remove = ucsi_stm32g0_remove,
756 .id_table = ucsi_stm32g0_typec_i2c_devid
757 };
758 module_i2c_driver(ucsi_stm32g0_i2c_driver);
759
760 MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@foss.st.com>");
761 MODULE_DESCRIPTION("STMicroelectronics STM32G0 Type-C controller");
762 MODULE_LICENSE("Dual BSD/GPL");
763 MODULE_ALIAS("platform:ucsi-stm32g0");
764