Skip to content

Commit 5d27a9c

Browse files
fschrempfbroonie
authored andcommitted
spi: spi-mem: Extend the SPI mem interface to set a custom memory name
When porting (Q)SPI controller drivers from the MTD layer to the SPI layer, the naming scheme for the memory devices changes. To be able to keep compatibility with the old drivers naming scheme, a name field is added to struct spi_mem and a hook is added to let controller drivers set a custom name for the memory device. Example for the FSL QSPI driver: Name with the old driver: 21e0000.qspi, or with multiple devices: 21e0000.qspi-0, 21e0000.qspi-1, ... Name with the new driver without spi_mem_get_name: spi4.0 Suggested-by: Boris Brezillon <[email protected]> Signed-off-by: Frieder Schrempf <[email protected]> Reviewed-by: Boris Brezillon <[email protected]> Signed-off-by: Mark Brown <[email protected]>
1 parent 06bcb51 commit 5d27a9c

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

drivers/spi/spi-mem.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,24 @@ int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
310310
}
311311
EXPORT_SYMBOL_GPL(spi_mem_exec_op);
312312

313+
/**
314+
* spi_mem_get_name() - Return the SPI mem device name to be used by the
315+
* upper layer if necessary
316+
* @mem: the SPI memory
317+
*
318+
* This function allows SPI mem users to retrieve the SPI mem device name.
319+
* It is useful if the upper layer needs to expose a custom name for
320+
* compatibility reasons.
321+
*
322+
* Return: a string containing the name of the memory device to be used
323+
* by the SPI mem user
324+
*/
325+
const char *spi_mem_get_name(struct spi_mem *mem)
326+
{
327+
return mem->name;
328+
}
329+
EXPORT_SYMBOL_GPL(spi_mem_get_name);
330+
313331
/**
314332
* spi_mem_adjust_op_size() - Adjust the data size of a SPI mem operation to
315333
* match controller limitations
@@ -344,13 +362,23 @@ static inline struct spi_mem_driver *to_spi_mem_drv(struct device_driver *drv)
344362
static int spi_mem_probe(struct spi_device *spi)
345363
{
346364
struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
365+
struct spi_controller *ctlr = spi->controller;
347366
struct spi_mem *mem;
348367

349368
mem = devm_kzalloc(&spi->dev, sizeof(*mem), GFP_KERNEL);
350369
if (!mem)
351370
return -ENOMEM;
352371

353372
mem->spi = spi;
373+
374+
if (ctlr->mem_ops && ctlr->mem_ops->get_name)
375+
mem->name = ctlr->mem_ops->get_name(mem);
376+
else
377+
mem->name = dev_name(&spi->dev);
378+
379+
if (IS_ERR_OR_NULL(mem->name))
380+
return PTR_ERR(mem->name);
381+
354382
spi_set_drvdata(spi, mem);
355383

356384
return memdrv->probe(mem);

include/linux/spi/spi-mem.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ struct spi_mem_op {
123123
* struct spi_mem - describes a SPI memory device
124124
* @spi: the underlying SPI device
125125
* @drvpriv: spi_mem_driver private data
126+
* @name: name of the SPI memory device
126127
*
127128
* Extra information that describe the SPI memory device and may be needed by
128129
* the controller to properly handle this device should be placed here.
@@ -133,6 +134,7 @@ struct spi_mem_op {
133134
struct spi_mem {
134135
struct spi_device *spi;
135136
void *drvpriv;
137+
char *name;
136138
};
137139

138140
/**
@@ -165,6 +167,13 @@ static inline void *spi_mem_get_drvdata(struct spi_mem *mem)
165167
* limitations)
166168
* @supports_op: check if an operation is supported by the controller
167169
* @exec_op: execute a SPI memory operation
170+
* @get_name: get a custom name for the SPI mem device from the controller.
171+
* This might be needed if the controller driver has been ported
172+
* to use the SPI mem layer and a custom name is used to keep
173+
* mtdparts compatible.
174+
* Note that if the implementation of this function allocates memory
175+
* dynamically, then it should do so with devm_xxx(), as we don't
176+
* have a ->free_name() function.
168177
*
169178
* This interface should be implemented by SPI controllers providing an
170179
* high-level interface to execute SPI memory operation, which is usually the
@@ -176,6 +185,7 @@ struct spi_controller_mem_ops {
176185
const struct spi_mem_op *op);
177186
int (*exec_op)(struct spi_mem *mem,
178187
const struct spi_mem_op *op);
188+
const char *(*get_name)(struct spi_mem *mem);
179189
};
180190

181191
/**
@@ -234,6 +244,8 @@ bool spi_mem_supports_op(struct spi_mem *mem,
234244
int spi_mem_exec_op(struct spi_mem *mem,
235245
const struct spi_mem_op *op);
236246

247+
const char *spi_mem_get_name(struct spi_mem *mem);
248+
237249
int spi_mem_driver_register_with_owner(struct spi_mem_driver *drv,
238250
struct module *owner);
239251

0 commit comments

Comments
 (0)