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