efe6e52720ade283971757e60976ad46e11a074a
[openwrt/staging/blogic.git] /
1 From 9008cdacdc41f8233f4444b86cf3a17201686e2d Mon Sep 17 00:00:00 2001
2 From: Shiji Yang <yangshiji66@outlook.com>
3 Date: Tue, 18 Jul 2023 20:18:16 +0800
4 Subject: [PATCH 5/5] wifi: rt2x00: support loading eeprom from NVMEM cells
5
6 This patch allows rt2x00 to load eeprom from "eeprom" NVMEM cell.
7
8 Example:
9
10 /* load eeprom from NVMEM provider 'eep' */
11 &wmac {
12 nvmem-cells = <&eep>;
13 nvmem-cell-names = "eeprom";
14 };
15
16 Signed-off-by: Shiji Yang <yangshiji66@outlook.com>
17 Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
18 ---
19 .../net/wireless/ralink/rt2x00/rt2x00eeprom.c | 41 ++++++++++++++++++-
20 1 file changed, 40 insertions(+), 1 deletion(-)
21
22 --- a/drivers/net/wireless/ralink/rt2x00/rt2x00eeprom.c
23 +++ b/drivers/net/wireless/ralink/rt2x00/rt2x00eeprom.c
24 @@ -14,12 +14,12 @@
25 #include <linux/mtd/mtd.h>
26 #include <linux/mtd/partitions.h>
27 #endif
28 +#include <linux/nvmem-consumer.h>
29 #include <linux/of.h>
30
31 #include "rt2x00.h"
32 #include "rt2x00soc.h"
33
34 -#if IS_ENABLED(CONFIG_MTD)
35 static void rt2800lib_eeprom_swap(struct rt2x00_dev *rt2x00dev)
36 {
37 struct device_node *np = rt2x00dev->dev->of_node;
38 @@ -33,6 +33,7 @@ static void rt2800lib_eeprom_swap(struct
39 rt2x00dev->eeprom[i] = swab16(rt2x00dev->eeprom[i]);
40 }
41
42 +#if IS_ENABLED(CONFIG_MTD)
43 static int rt2800lib_read_eeprom_mtd(struct rt2x00_dev *rt2x00dev)
44 {
45 int ret = -EINVAL;
46 @@ -88,6 +89,40 @@ static int rt2800lib_read_eeprom_mtd(str
47 }
48 #endif
49
50 +static int rt2800lib_read_eeprom_nvmem(struct rt2x00_dev *rt2x00dev)
51 +{
52 + struct device_node *np = rt2x00dev->dev->of_node;
53 + unsigned int len = rt2x00dev->ops->eeprom_size;
54 + struct nvmem_cell *cell;
55 + const void *data;
56 + size_t retlen;
57 + int ret = 0;
58 +
59 + cell = of_nvmem_cell_get(np, "eeprom");
60 + if (IS_ERR(cell))
61 + return PTR_ERR(cell);
62 +
63 + data = nvmem_cell_read(cell, &retlen);
64 + nvmem_cell_put(cell);
65 +
66 + if (IS_ERR(data))
67 + return PTR_ERR(data);
68 +
69 + if (retlen != len) {
70 + dev_err(rt2x00dev->dev, "invalid eeprom size, required: 0x%04x\n", len);
71 + ret = -EINVAL;
72 + goto exit;
73 + }
74 +
75 + memcpy(rt2x00dev->eeprom, data, len);
76 +
77 + rt2800lib_eeprom_swap(rt2x00dev);
78 +
79 +exit:
80 + kfree(data);
81 + return ret;
82 +}
83 +
84 static const char *
85 rt2x00lib_get_eeprom_file_name(struct rt2x00_dev *rt2x00dev)
86 {
87 @@ -164,5 +199,9 @@ int rt2x00lib_read_eeprom(struct rt2x00_
88 return 0;
89 #endif
90
91 + ret = rt2800lib_read_eeprom_nvmem(rt2x00dev);
92 + if (!ret)
93 + return 0;
94 +
95 return rt2x00lib_read_eeprom_file(rt2x00dev);
96 }