aboutsummaryrefslogtreecommitdiff
path: root/drivers/fsi/i2cr-scom.c
blob: cb7e02213032ccb86d4c1267266c4b3ecdaf0c0a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// SPDX-License-Identifier: GPL-2.0
/* Copyright (C) IBM Corporation 2023 */

#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/fsi.h>
#include <linux/module.h>
#include <linux/mod_devicetable.h>

#include "fsi-master-i2cr.h"
#include "fsi-slave.h"

struct i2cr_scom {
	struct device dev;
	struct cdev cdev;
	struct fsi_master_i2cr *i2cr;
};

static loff_t i2cr_scom_llseek(struct file *file, loff_t offset, int whence)
{
	switch (whence) {
	case SEEK_CUR:
		break;
	case SEEK_SET:
		file->f_pos = offset;
		break;
	default:
		return -EINVAL;
	}

	return offset;
}

static ssize_t i2cr_scom_read(struct file *filep, char __user *buf, size_t len, loff_t *offset)
{
	struct i2cr_scom *scom = filep->private_data;
	u64 data;
	int ret;

	if (len != sizeof(data))
		return -EINVAL;

	ret = fsi_master_i2cr_read(scom->i2cr, (u32)*offset, &data);
	if (ret)
		return ret;

	ret = copy_to_user(buf, &data, len);
	if (ret)
		return ret;

	return len;
}

static ssize_t i2cr_scom_write(struct file *filep, const char __user *buf, size_t len,
			       loff_t *offset)
{
	struct i2cr_scom *scom = filep->private_data;
	u64 data;
	int ret;

	if (len != sizeof(data))
		return -EINVAL;

	ret = copy_from_user(&data, buf, len);
	if (ret)
		return ret;

	ret = fsi_master_i2cr_write(scom->i2cr, (u32)*offset, data);
	if (ret)
		return ret;

	return len;
}

static const struct file_operations i2cr_scom_fops = {
	.owner		= THIS_MODULE,
	.open		= simple_open,
	.llseek		= i2cr_scom_llseek,
	.read		= i2cr_scom_read,
	.write		= i2cr_scom_write,
};

static int i2cr_scom_probe(struct device *dev)
{
	struct fsi_device *fsi_dev = to_fsi_dev(dev);
	struct i2cr_scom *scom;
	int didx;
	int ret;

	if (!is_fsi_master_i2cr(fsi_dev->slave->master))
		return -ENODEV;

	scom = devm_kzalloc(dev, sizeof(*scom), GFP_KERNEL);
	if (!scom)
		return -ENOMEM;

	scom->i2cr = to_fsi_master_i2cr(fsi_dev->slave->master);
	dev_set_drvdata(dev, scom);

	scom->dev.type = &fsi_cdev_type;
	scom->dev.parent = dev;
	device_initialize(&scom->dev);

	ret = fsi_get_new_minor(fsi_dev, fsi_dev_scom, &scom->dev.devt, &didx);
	if (ret)
		return ret;

	dev_set_name(&scom->dev, "scom%d", didx);
	cdev_init(&scom->cdev, &i2cr_scom_fops);
	ret = cdev_device_add(&scom->cdev, &scom->dev);
	if (ret)
		fsi_free_minor(scom->dev.devt);

	return ret;
}

static int i2cr_scom_remove(struct device *dev)
{
	struct i2cr_scom *scom = dev_get_drvdata(dev);

	cdev_device_del(&scom->cdev, &scom->dev);
	fsi_free_minor(scom->dev.devt);

	return 0;
}

static const struct of_device_id i2cr_scom_of_ids[] = {
	{ .compatible = "ibm,i2cr-scom" },
	{ }
};
MODULE_DEVICE_TABLE(of, i2cr_scom_of_ids);

static const struct fsi_device_id i2cr_scom_ids[] = {
	{ 0x5, FSI_VERSION_ANY },
	{ }
};

static struct fsi_driver i2cr_scom_driver = {
	.id_table = i2cr_scom_ids,
	.drv = {
		.name = "i2cr_scom",
		.bus = &fsi_bus_type,
		.of_match_table = i2cr_scom_of_ids,
		.probe = i2cr_scom_probe,
		.remove = i2cr_scom_remove,
	}
};

module_fsi_driver(i2cr_scom_driver);

MODULE_AUTHOR("Eddie James <eajames@linux.ibm.com>");
MODULE_DESCRIPTION("IBM I2C Responder SCOM driver");
MODULE_LICENSE("GPL");