1 // SPDX-License-Identifier: GPL-2.0+
4 * EEPROM driver for RAVE SP
6 * Copyright (C) 2018 Zodiac Inflight Innovations
9 #include <linux/kernel.h>
10 #include <linux/mfd/rave-sp.h>
11 #include <linux/module.h>
12 #include <linux/nvmem-provider.h>
13 #include <linux/of_device.h>
14 #include <linux/platform_device.h>
15 #include <linux/sizes.h>
18 * enum rave_sp_eeprom_access_type - Supported types of EEPROM access
20 * @RAVE_SP_EEPROM_WRITE: EEPROM write
21 * @RAVE_SP_EEPROM_READ: EEPROM read
23 enum rave_sp_eeprom_access_type {
24 RAVE_SP_EEPROM_WRITE = 0,
25 RAVE_SP_EEPROM_READ = 1,
29 * enum rave_sp_eeprom_header_size - EEPROM command header sizes
31 * @RAVE_SP_EEPROM_HEADER_SMALL: EEPROM header size for "small" devices (< 8K)
32 * @RAVE_SP_EEPROM_HEADER_BIG: EEPROM header size for "big" devices (> 8K)
34 enum rave_sp_eeprom_header_size {
35 RAVE_SP_EEPROM_HEADER_SMALL = 4U,
36 RAVE_SP_EEPROM_HEADER_BIG = 5U,
39 #define RAVE_SP_EEPROM_PAGE_SIZE 32U
42 * struct rave_sp_eeprom_page - RAVE SP EEPROM page
44 * @type: Access type (see enum rave_sp_eeprom_access_type)
45 * @success: Success flag (Success = 1, Failure = 0)
48 * Note this structure corresponds to RSP_*_EEPROM payload from RAVE
51 struct rave_sp_eeprom_page {
54 u8 data[RAVE_SP_EEPROM_PAGE_SIZE];
58 * struct rave_sp_eeprom - RAVE SP EEPROM device
60 * @sp: Pointer to parent RAVE SP device
61 * @mutex: Lock protecting access to EEPROM
62 * @address: EEPROM device address
63 * @header_size: Size of EEPROM command header for this device
64 * @dev: Pointer to corresponding struct device used for logging
66 struct rave_sp_eeprom {
70 unsigned int header_size;
75 * rave_sp_eeprom_io - Low-level part of EEPROM page access
77 * @eeprom: EEPROM device to write to
78 * @type: EEPROM access type (read or write)
79 * @idx: number of the EEPROM page
80 * @page: Data to write or buffer to store result (via page->data)
82 * This function does all of the low-level work required to perform a
83 * EEPROM access. This includes formatting correct command payload,
84 * sending it and checking received results.
86 * Returns zero in case of success or negative error code in
89 static int rave_sp_eeprom_io(struct rave_sp_eeprom *eeprom,
90 enum rave_sp_eeprom_access_type type,
92 struct rave_sp_eeprom_page *page)
94 const bool is_write = type == RAVE_SP_EEPROM_WRITE;
95 const unsigned int data_size = is_write ? sizeof(page->data) : 0;
96 const unsigned int cmd_size = eeprom->header_size + data_size;
97 const unsigned int rsp_size =
98 is_write ? sizeof(*page) - sizeof(page->data) : sizeof(*page);
99 unsigned int offset = 0;
103 cmd[offset++] = eeprom->address;
105 cmd[offset++] = type;
109 * If there's still room in this command's header it means we
110 * are talkin to EEPROM that uses 16-bit page numbers and we
111 * have to specify index's MSB in payload as well.
113 if (offset < eeprom->header_size)
114 cmd[offset++] = idx >> 8;
116 * Copy our data to write to command buffer first. In case of
117 * a read data_size should be zero and memcpy would become a
120 memcpy(&cmd[offset], page->data, data_size);
122 ret = rave_sp_exec(eeprom->sp, cmd, cmd_size, page, rsp_size);
126 if (page->type != type)
136 * rave_sp_eeprom_page_access - Access single EEPROM page
138 * @eeprom: EEPROM device to access
139 * @type: Access type to perform (read or write)
140 * @offset: Offset within EEPROM to access
142 * @data_len: Size of the data buffer
144 * This function performs a generic access to a single page or a
145 * portion thereof. Requested access MUST NOT cross the EEPROM page
148 * Returns zero in case of success or negative error code in
152 rave_sp_eeprom_page_access(struct rave_sp_eeprom *eeprom,
153 enum rave_sp_eeprom_access_type type,
154 unsigned int offset, u8 *data,
157 const unsigned int page_offset = offset % RAVE_SP_EEPROM_PAGE_SIZE;
158 const unsigned int page_nr = offset / RAVE_SP_EEPROM_PAGE_SIZE;
159 struct rave_sp_eeprom_page page;
163 * This function will not work if data access we've been asked
164 * to do is crossing EEPROM page boundary. Normally this
165 * should never happen and getting here would indicate a bug
168 if (WARN_ON(data_len > sizeof(page.data) - page_offset))
171 if (type == RAVE_SP_EEPROM_WRITE) {
173 * If doing a partial write we need to do a read first
174 * to fill the rest of the page with correct data.
176 if (data_len < RAVE_SP_EEPROM_PAGE_SIZE) {
177 ret = rave_sp_eeprom_io(eeprom, RAVE_SP_EEPROM_READ,
183 memcpy(&page.data[page_offset], data, data_len);
186 ret = rave_sp_eeprom_io(eeprom, type, page_nr, &page);
191 * Since we receive the result of the read via 'page.data'
192 * buffer we need to copy that to 'data'
194 if (type == RAVE_SP_EEPROM_READ)
195 memcpy(data, &page.data[page_offset], data_len);
201 * rave_sp_eeprom_access - Access EEPROM data
203 * @eeprom: EEPROM device to access
204 * @type: Access type to perform (read or write)
205 * @offset: Offset within EEPROM to access
207 * @data_len: Size of the data buffer
209 * This function performs a generic access (either read or write) at
210 * arbitrary offset (not necessary page aligned) of arbitrary length
211 * (is not constrained by EEPROM page size).
213 * Returns zero in case of success or negative error code in case of
216 static int rave_sp_eeprom_access(struct rave_sp_eeprom *eeprom,
217 enum rave_sp_eeprom_access_type type,
218 unsigned int offset, u8 *data,
219 unsigned int data_len)
221 unsigned int residue;
226 mutex_lock(&eeprom->mutex);
228 head = offset % RAVE_SP_EEPROM_PAGE_SIZE;
233 * First iteration, if we are doing an access that is
234 * not 32-byte aligned, we need to access only data up
235 * to a page boundary to avoid corssing it in
236 * rave_sp_eeprom_page_access()
238 if (unlikely(head)) {
239 chunk = RAVE_SP_EEPROM_PAGE_SIZE - head;
241 * This can only happen once per
242 * rave_sp_eeprom_access() call, so we set
243 * head to zero to process all the other
244 * iterations normally.
248 chunk = RAVE_SP_EEPROM_PAGE_SIZE;
252 * We should never read more that 'residue' bytes
254 chunk = min(chunk, residue);
255 ret = rave_sp_eeprom_page_access(eeprom, type, offset,
265 mutex_unlock(&eeprom->mutex);
269 static int rave_sp_eeprom_reg_read(void *eeprom, unsigned int offset,
270 void *val, size_t bytes)
272 return rave_sp_eeprom_access(eeprom, RAVE_SP_EEPROM_READ,
276 static int rave_sp_eeprom_reg_write(void *eeprom, unsigned int offset,
277 void *val, size_t bytes)
279 return rave_sp_eeprom_access(eeprom, RAVE_SP_EEPROM_WRITE,
283 static int rave_sp_eeprom_probe(struct platform_device *pdev)
285 struct device *dev = &pdev->dev;
286 struct rave_sp *sp = dev_get_drvdata(dev->parent);
287 struct device_node *np = dev->of_node;
288 struct nvmem_config config = { 0 };
289 struct rave_sp_eeprom *eeprom;
290 struct nvmem_device *nvmem;
293 if (of_property_read_u32_array(np, "reg", reg, ARRAY_SIZE(reg))) {
294 dev_err(dev, "Failed to parse \"reg\" property\n");
300 * Per ICD, we have no more than 2 bytes to specify EEPROM
303 if (size > U16_MAX * RAVE_SP_EEPROM_PAGE_SIZE) {
304 dev_err(dev, "Specified size is too big\n");
308 eeprom = devm_kzalloc(dev, sizeof(*eeprom), GFP_KERNEL);
312 eeprom->address = reg[0];
317 eeprom->header_size = RAVE_SP_EEPROM_HEADER_BIG;
319 eeprom->header_size = RAVE_SP_EEPROM_HEADER_SMALL;
321 mutex_init(&eeprom->mutex);
324 of_property_read_string(np, "zii,eeprom-name", &config.name);
325 config.priv = eeprom;
328 config.reg_read = rave_sp_eeprom_reg_read;
329 config.reg_write = rave_sp_eeprom_reg_write;
330 config.word_size = 1;
333 nvmem = devm_nvmem_register(dev, &config);
335 return PTR_ERR_OR_ZERO(nvmem);
338 static const struct of_device_id rave_sp_eeprom_of_match[] = {
339 { .compatible = "zii,rave-sp-eeprom" },
342 MODULE_DEVICE_TABLE(of, rave_sp_eeprom_of_match);
344 static struct platform_driver rave_sp_eeprom_driver = {
345 .probe = rave_sp_eeprom_probe,
347 .name = KBUILD_MODNAME,
348 .of_match_table = rave_sp_eeprom_of_match,
351 module_platform_driver(rave_sp_eeprom_driver);
353 MODULE_LICENSE("GPL");
354 MODULE_AUTHOR("Andrey Vostrikov <andrey.vostrikov@cogentembedded.com>");
355 MODULE_AUTHOR("Nikita Yushchenko <nikita.yoush@cogentembedded.com>");
356 MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
357 MODULE_DESCRIPTION("RAVE SP EEPROM driver");