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