xref: /linux/drivers/mtd/spi-nor/sysfs.c (revision 353b7a55dcaf5fb8758e09ebe2ddf5f3adbac7c5)
1*36ac0228SMichael Walle // SPDX-License-Identifier: GPL-2.0
2*36ac0228SMichael Walle 
3*36ac0228SMichael Walle #include <linux/mtd/spi-nor.h>
4*36ac0228SMichael Walle #include <linux/spi/spi.h>
5*36ac0228SMichael Walle #include <linux/spi/spi-mem.h>
6*36ac0228SMichael Walle #include <linux/sysfs.h>
7*36ac0228SMichael Walle 
8*36ac0228SMichael Walle #include "core.h"
9*36ac0228SMichael Walle 
10*36ac0228SMichael Walle static ssize_t manufacturer_show(struct device *dev,
11*36ac0228SMichael Walle 				 struct device_attribute *attr, char *buf)
12*36ac0228SMichael Walle {
13*36ac0228SMichael Walle 	struct spi_device *spi = to_spi_device(dev);
14*36ac0228SMichael Walle 	struct spi_mem *spimem = spi_get_drvdata(spi);
15*36ac0228SMichael Walle 	struct spi_nor *nor = spi_mem_get_drvdata(spimem);
16*36ac0228SMichael Walle 
17*36ac0228SMichael Walle 	return sysfs_emit(buf, "%s\n", nor->manufacturer->name);
18*36ac0228SMichael Walle }
19*36ac0228SMichael Walle static DEVICE_ATTR_RO(manufacturer);
20*36ac0228SMichael Walle 
21*36ac0228SMichael Walle static ssize_t partname_show(struct device *dev,
22*36ac0228SMichael Walle 			     struct device_attribute *attr, char *buf)
23*36ac0228SMichael Walle {
24*36ac0228SMichael Walle 	struct spi_device *spi = to_spi_device(dev);
25*36ac0228SMichael Walle 	struct spi_mem *spimem = spi_get_drvdata(spi);
26*36ac0228SMichael Walle 	struct spi_nor *nor = spi_mem_get_drvdata(spimem);
27*36ac0228SMichael Walle 
28*36ac0228SMichael Walle 	return sysfs_emit(buf, "%s\n", nor->info->name);
29*36ac0228SMichael Walle }
30*36ac0228SMichael Walle static DEVICE_ATTR_RO(partname);
31*36ac0228SMichael Walle 
32*36ac0228SMichael Walle static ssize_t jedec_id_show(struct device *dev,
33*36ac0228SMichael Walle 			     struct device_attribute *attr, char *buf)
34*36ac0228SMichael Walle {
35*36ac0228SMichael Walle 	struct spi_device *spi = to_spi_device(dev);
36*36ac0228SMichael Walle 	struct spi_mem *spimem = spi_get_drvdata(spi);
37*36ac0228SMichael Walle 	struct spi_nor *nor = spi_mem_get_drvdata(spimem);
38*36ac0228SMichael Walle 
39*36ac0228SMichael Walle 	return sysfs_emit(buf, "%*phN\n", nor->info->id_len, nor->info->id);
40*36ac0228SMichael Walle }
41*36ac0228SMichael Walle static DEVICE_ATTR_RO(jedec_id);
42*36ac0228SMichael Walle 
43*36ac0228SMichael Walle static struct attribute *spi_nor_sysfs_entries[] = {
44*36ac0228SMichael Walle 	&dev_attr_manufacturer.attr,
45*36ac0228SMichael Walle 	&dev_attr_partname.attr,
46*36ac0228SMichael Walle 	&dev_attr_jedec_id.attr,
47*36ac0228SMichael Walle 	NULL
48*36ac0228SMichael Walle };
49*36ac0228SMichael Walle 
50*36ac0228SMichael Walle static ssize_t sfdp_read(struct file *filp, struct kobject *kobj,
51*36ac0228SMichael Walle 			 struct bin_attribute *bin_attr, char *buf,
52*36ac0228SMichael Walle 			 loff_t off, size_t count)
53*36ac0228SMichael Walle {
54*36ac0228SMichael Walle 	struct spi_device *spi = to_spi_device(kobj_to_dev(kobj));
55*36ac0228SMichael Walle 	struct spi_mem *spimem = spi_get_drvdata(spi);
56*36ac0228SMichael Walle 	struct spi_nor *nor = spi_mem_get_drvdata(spimem);
57*36ac0228SMichael Walle 	struct sfdp *sfdp = nor->sfdp;
58*36ac0228SMichael Walle 	size_t sfdp_size = sfdp->num_dwords * sizeof(*sfdp->dwords);
59*36ac0228SMichael Walle 
60*36ac0228SMichael Walle 	return memory_read_from_buffer(buf, count, &off, nor->sfdp->dwords,
61*36ac0228SMichael Walle 				       sfdp_size);
62*36ac0228SMichael Walle }
63*36ac0228SMichael Walle static BIN_ATTR_RO(sfdp, 0);
64*36ac0228SMichael Walle 
65*36ac0228SMichael Walle static struct bin_attribute *spi_nor_sysfs_bin_entries[] = {
66*36ac0228SMichael Walle 	&bin_attr_sfdp,
67*36ac0228SMichael Walle 	NULL
68*36ac0228SMichael Walle };
69*36ac0228SMichael Walle 
70*36ac0228SMichael Walle static umode_t spi_nor_sysfs_is_bin_visible(struct kobject *kobj,
71*36ac0228SMichael Walle 					    struct bin_attribute *attr, int n)
72*36ac0228SMichael Walle {
73*36ac0228SMichael Walle 	struct spi_device *spi = to_spi_device(kobj_to_dev(kobj));
74*36ac0228SMichael Walle 	struct spi_mem *spimem = spi_get_drvdata(spi);
75*36ac0228SMichael Walle 	struct spi_nor *nor = spi_mem_get_drvdata(spimem);
76*36ac0228SMichael Walle 
77*36ac0228SMichael Walle 	if (attr == &bin_attr_sfdp && nor->sfdp)
78*36ac0228SMichael Walle 		return 0444;
79*36ac0228SMichael Walle 
80*36ac0228SMichael Walle 	return 0;
81*36ac0228SMichael Walle }
82*36ac0228SMichael Walle 
83*36ac0228SMichael Walle static const struct attribute_group spi_nor_sysfs_group = {
84*36ac0228SMichael Walle 	.name		= "spi-nor",
85*36ac0228SMichael Walle 	.is_bin_visible	= spi_nor_sysfs_is_bin_visible,
86*36ac0228SMichael Walle 	.attrs		= spi_nor_sysfs_entries,
87*36ac0228SMichael Walle 	.bin_attrs	= spi_nor_sysfs_bin_entries,
88*36ac0228SMichael Walle };
89*36ac0228SMichael Walle 
90*36ac0228SMichael Walle const struct attribute_group *spi_nor_sysfs_groups[] = {
91*36ac0228SMichael Walle 	&spi_nor_sysfs_group,
92*36ac0228SMichael Walle 	NULL
93*36ac0228SMichael Walle };
94