136ac0228SMichael Walle // SPDX-License-Identifier: GPL-2.0
236ac0228SMichael Walle
336ac0228SMichael Walle #include <linux/mtd/spi-nor.h>
436ac0228SMichael Walle #include <linux/spi/spi.h>
536ac0228SMichael Walle #include <linux/spi/spi-mem.h>
636ac0228SMichael Walle #include <linux/sysfs.h>
736ac0228SMichael Walle
836ac0228SMichael Walle #include "core.h"
936ac0228SMichael Walle
manufacturer_show(struct device * dev,struct device_attribute * attr,char * buf)1036ac0228SMichael Walle static ssize_t manufacturer_show(struct device *dev,
1136ac0228SMichael Walle struct device_attribute *attr, char *buf)
1236ac0228SMichael Walle {
1336ac0228SMichael Walle struct spi_device *spi = to_spi_device(dev);
1436ac0228SMichael Walle struct spi_mem *spimem = spi_get_drvdata(spi);
1536ac0228SMichael Walle struct spi_nor *nor = spi_mem_get_drvdata(spimem);
1636ac0228SMichael Walle
1736ac0228SMichael Walle return sysfs_emit(buf, "%s\n", nor->manufacturer->name);
1836ac0228SMichael Walle }
1936ac0228SMichael Walle static DEVICE_ATTR_RO(manufacturer);
2036ac0228SMichael Walle
partname_show(struct device * dev,struct device_attribute * attr,char * buf)2136ac0228SMichael Walle static ssize_t partname_show(struct device *dev,
2236ac0228SMichael Walle struct device_attribute *attr, char *buf)
2336ac0228SMichael Walle {
2436ac0228SMichael Walle struct spi_device *spi = to_spi_device(dev);
2536ac0228SMichael Walle struct spi_mem *spimem = spi_get_drvdata(spi);
2636ac0228SMichael Walle struct spi_nor *nor = spi_mem_get_drvdata(spimem);
2736ac0228SMichael Walle
2836ac0228SMichael Walle return sysfs_emit(buf, "%s\n", nor->info->name);
2936ac0228SMichael Walle }
3036ac0228SMichael Walle static DEVICE_ATTR_RO(partname);
3136ac0228SMichael Walle
jedec_id_show(struct device * dev,struct device_attribute * attr,char * buf)3236ac0228SMichael Walle static ssize_t jedec_id_show(struct device *dev,
3336ac0228SMichael Walle struct device_attribute *attr, char *buf)
3436ac0228SMichael Walle {
3536ac0228SMichael Walle struct spi_device *spi = to_spi_device(dev);
3636ac0228SMichael Walle struct spi_mem *spimem = spi_get_drvdata(spi);
3736ac0228SMichael Walle struct spi_nor *nor = spi_mem_get_drvdata(spimem);
38*0a92de16SMichael Walle const u8 *id = nor->info->id_len ? nor->info->id : nor->id;
39*0a92de16SMichael Walle u8 id_len = nor->info->id_len ?: SPI_NOR_MAX_ID_LEN;
4036ac0228SMichael Walle
41*0a92de16SMichael Walle return sysfs_emit(buf, "%*phN\n", id_len, id);
4236ac0228SMichael Walle }
4336ac0228SMichael Walle static DEVICE_ATTR_RO(jedec_id);
4436ac0228SMichael Walle
4536ac0228SMichael Walle static struct attribute *spi_nor_sysfs_entries[] = {
4636ac0228SMichael Walle &dev_attr_manufacturer.attr,
4736ac0228SMichael Walle &dev_attr_partname.attr,
4836ac0228SMichael Walle &dev_attr_jedec_id.attr,
4936ac0228SMichael Walle NULL
5036ac0228SMichael Walle };
5136ac0228SMichael Walle
sfdp_read(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)5236ac0228SMichael Walle static ssize_t sfdp_read(struct file *filp, struct kobject *kobj,
5336ac0228SMichael Walle struct bin_attribute *bin_attr, char *buf,
5436ac0228SMichael Walle loff_t off, size_t count)
5536ac0228SMichael Walle {
5636ac0228SMichael Walle struct spi_device *spi = to_spi_device(kobj_to_dev(kobj));
5736ac0228SMichael Walle struct spi_mem *spimem = spi_get_drvdata(spi);
5836ac0228SMichael Walle struct spi_nor *nor = spi_mem_get_drvdata(spimem);
5936ac0228SMichael Walle struct sfdp *sfdp = nor->sfdp;
6036ac0228SMichael Walle size_t sfdp_size = sfdp->num_dwords * sizeof(*sfdp->dwords);
6136ac0228SMichael Walle
6236ac0228SMichael Walle return memory_read_from_buffer(buf, count, &off, nor->sfdp->dwords,
6336ac0228SMichael Walle sfdp_size);
6436ac0228SMichael Walle }
6536ac0228SMichael Walle static BIN_ATTR_RO(sfdp, 0);
6636ac0228SMichael Walle
6736ac0228SMichael Walle static struct bin_attribute *spi_nor_sysfs_bin_entries[] = {
6836ac0228SMichael Walle &bin_attr_sfdp,
6936ac0228SMichael Walle NULL
7036ac0228SMichael Walle };
7136ac0228SMichael Walle
spi_nor_sysfs_is_visible(struct kobject * kobj,struct attribute * attr,int n)727d388551SMichael Walle static umode_t spi_nor_sysfs_is_visible(struct kobject *kobj,
737d388551SMichael Walle struct attribute *attr, int n)
747d388551SMichael Walle {
757d388551SMichael Walle struct spi_device *spi = to_spi_device(kobj_to_dev(kobj));
767d388551SMichael Walle struct spi_mem *spimem = spi_get_drvdata(spi);
777d388551SMichael Walle struct spi_nor *nor = spi_mem_get_drvdata(spimem);
787d388551SMichael Walle
790d9270f2SMichael Walle if (attr == &dev_attr_manufacturer.attr && !nor->manufacturer)
800d9270f2SMichael Walle return 0;
81*0a92de16SMichael Walle if (attr == &dev_attr_jedec_id.attr && !nor->info->id_len && !nor->id)
827d388551SMichael Walle return 0;
837d388551SMichael Walle
847d388551SMichael Walle return 0444;
857d388551SMichael Walle }
867d388551SMichael Walle
spi_nor_sysfs_is_bin_visible(struct kobject * kobj,struct bin_attribute * attr,int n)8736ac0228SMichael Walle static umode_t spi_nor_sysfs_is_bin_visible(struct kobject *kobj,
8836ac0228SMichael Walle struct bin_attribute *attr, int n)
8936ac0228SMichael Walle {
9036ac0228SMichael Walle struct spi_device *spi = to_spi_device(kobj_to_dev(kobj));
9136ac0228SMichael Walle struct spi_mem *spimem = spi_get_drvdata(spi);
9236ac0228SMichael Walle struct spi_nor *nor = spi_mem_get_drvdata(spimem);
9336ac0228SMichael Walle
9436ac0228SMichael Walle if (attr == &bin_attr_sfdp && nor->sfdp)
9536ac0228SMichael Walle return 0444;
9636ac0228SMichael Walle
9736ac0228SMichael Walle return 0;
9836ac0228SMichael Walle }
9936ac0228SMichael Walle
10036ac0228SMichael Walle static const struct attribute_group spi_nor_sysfs_group = {
10136ac0228SMichael Walle .name = "spi-nor",
1027d388551SMichael Walle .is_visible = spi_nor_sysfs_is_visible,
10336ac0228SMichael Walle .is_bin_visible = spi_nor_sysfs_is_bin_visible,
10436ac0228SMichael Walle .attrs = spi_nor_sysfs_entries,
10536ac0228SMichael Walle .bin_attrs = spi_nor_sysfs_bin_entries,
10636ac0228SMichael Walle };
10736ac0228SMichael Walle
10836ac0228SMichael Walle const struct attribute_group *spi_nor_sysfs_groups[] = {
10936ac0228SMichael Walle &spi_nor_sysfs_group,
11036ac0228SMichael Walle NULL
11136ac0228SMichael Walle };
112