Skip to content

Commit b985f4c

Browse files
brglgregkh
authored andcommitted
nvmem: add support for cell info
Add new structs and routines allowing users to define nvmem cells from machine code. This global list of entries is parsed when a provider is registered and cells are associated with the relevant nvmem_device struct. A possible improvement for the future is to allow users to register cell tables after the nvmem provider has been registered by updating the cell list at each call to nvmem_(add|del)_cell_table(). Signed-off-by: Bartosz Golaszewski <[email protected]> Signed-off-by: Srinivas Kandagatla <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent c7235ee commit b985f4c

File tree

2 files changed

+96
-9
lines changed

2 files changed

+96
-9
lines changed

drivers/nvmem/core.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ struct nvmem_cell {
5959
static DEFINE_MUTEX(nvmem_mutex);
6060
static DEFINE_IDA(nvmem_ida);
6161

62+
static DEFINE_MUTEX(nvmem_cell_mutex);
63+
static LIST_HEAD(nvmem_cell_tables);
64+
6265
#ifdef CONFIG_DEBUG_LOCK_ALLOC
6366
static struct lock_class_key eeprom_lock_key;
6467
#endif
@@ -416,6 +419,43 @@ static int nvmem_setup_compat(struct nvmem_device *nvmem,
416419
return 0;
417420
}
418421

422+
static int nvmem_add_cells_from_table(struct nvmem_device *nvmem)
423+
{
424+
const struct nvmem_cell_info *info;
425+
struct nvmem_cell_table *table;
426+
struct nvmem_cell *cell;
427+
int rval = 0, i;
428+
429+
mutex_lock(&nvmem_cell_mutex);
430+
list_for_each_entry(table, &nvmem_cell_tables, node) {
431+
if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) {
432+
for (i = 0; i < table->ncells; i++) {
433+
info = &table->cells[i];
434+
435+
cell = kzalloc(sizeof(*cell), GFP_KERNEL);
436+
if (!cell) {
437+
rval = -ENOMEM;
438+
goto out;
439+
}
440+
441+
rval = nvmem_cell_info_to_nvmem_cell(nvmem,
442+
info,
443+
cell);
444+
if (rval) {
445+
kfree(cell);
446+
goto out;
447+
}
448+
449+
nvmem_cell_add(cell);
450+
}
451+
}
452+
}
453+
454+
out:
455+
mutex_unlock(&nvmem_cell_mutex);
456+
return rval;
457+
}
458+
419459
/**
420460
* nvmem_register() - Register a nvmem device for given nvmem_config.
421461
* Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
@@ -502,8 +542,14 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
502542
goto err_teardown_compat;
503543
}
504544

545+
rval = nvmem_add_cells_from_table(nvmem);
546+
if (rval)
547+
goto err_remove_cells;
548+
505549
return nvmem;
506550

551+
err_remove_cells:
552+
nvmem_device_remove_all_cells(nvmem);
507553
err_teardown_compat:
508554
if (config->compat)
509555
device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
@@ -1306,6 +1352,32 @@ int nvmem_device_write(struct nvmem_device *nvmem,
13061352
}
13071353
EXPORT_SYMBOL_GPL(nvmem_device_write);
13081354

1355+
/**
1356+
* nvmem_add_cell_table() - register a table of cell info entries
1357+
*
1358+
* @table: table of cell info entries
1359+
*/
1360+
void nvmem_add_cell_table(struct nvmem_cell_table *table)
1361+
{
1362+
mutex_lock(&nvmem_cell_mutex);
1363+
list_add_tail(&table->node, &nvmem_cell_tables);
1364+
mutex_unlock(&nvmem_cell_mutex);
1365+
}
1366+
EXPORT_SYMBOL_GPL(nvmem_add_cell_table);
1367+
1368+
/**
1369+
* nvmem_del_cell_table() - remove a previously registered cell info table
1370+
*
1371+
* @table: table of cell info entries
1372+
*/
1373+
void nvmem_del_cell_table(struct nvmem_cell_table *table)
1374+
{
1375+
mutex_lock(&nvmem_cell_mutex);
1376+
list_del(&table->node);
1377+
mutex_unlock(&nvmem_cell_mutex);
1378+
}
1379+
EXPORT_SYMBOL_GPL(nvmem_del_cell_table);
1380+
13091381
/**
13101382
* nvmem_dev_name() - Get the name of a given nvmem device.
13111383
*

include/linux/nvmem-provider.h

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,25 @@ struct nvmem_config {
6767
struct device *base_dev;
6868
};
6969

70+
/**
71+
* struct nvmem_cell_table - NVMEM cell definitions for given provider
72+
*
73+
* @nvmem_name: Provider name.
74+
* @cells: Array of cell definitions.
75+
* @ncells: Number of cell definitions in the array.
76+
* @node: List node.
77+
*
78+
* This structure together with related helper functions is provided for users
79+
* that don't can't access the nvmem provided structure but wish to register
80+
* cell definitions for it e.g. board files registering an EEPROM device.
81+
*/
82+
struct nvmem_cell_table {
83+
const char *nvmem_name;
84+
const struct nvmem_cell_info *cells;
85+
size_t ncells;
86+
struct list_head node;
87+
};
88+
7089
#if IS_ENABLED(CONFIG_NVMEM)
7190

7291
struct nvmem_device *nvmem_register(const struct nvmem_config *cfg);
@@ -77,9 +96,9 @@ struct nvmem_device *devm_nvmem_register(struct device *dev,
7796

7897
int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem);
7998

80-
int nvmem_add_cells(struct nvmem_device *nvmem,
81-
const struct nvmem_cell_info *info,
82-
int ncells);
99+
void nvmem_add_cell_table(struct nvmem_cell_table *table);
100+
void nvmem_del_cell_table(struct nvmem_cell_table *table);
101+
83102
#else
84103

85104
static inline struct nvmem_device *nvmem_register(const struct nvmem_config *c)
@@ -102,12 +121,8 @@ devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem)
102121

103122
}
104123

105-
static inline int nvmem_add_cells(struct nvmem_device *nvmem,
106-
const struct nvmem_cell_info *info,
107-
int ncells)
108-
{
109-
return -ENOSYS;
110-
}
124+
static inline void nvmem_add_cell_table(struct nvmem_cell_table *table) {}
125+
static inline void nvmem_del_cell_table(struct nvmem_cell_table *table) {}
111126

112127
#endif /* CONFIG_NVMEM */
113128
#endif /* ifndef _LINUX_NVMEM_PROVIDER_H */

0 commit comments

Comments
 (0)