summaryrefslogtreecommitdiff
path: root/drivers/hwmon/max17135-hwmon.c
blob: b0a9ff432f9a1b34ca5bcb0c2c4c455e637ac1fb (plain)
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
155
156
157
158
159
160
161
162
163
164
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2010-2015 Freescale Semiconductor, Inc.
 * Copyright 2019 NXP
 */
/*
 * max17135.c
 *
 * Based on the MAX1619 driver.
 * Copyright (C) 2003-2004 Alexey Fisher <fishor@mail.ru>
 *                         Jean Delvare <khali@linux-fr.org>
 *
 * The MAX17135 is a sensor chip made by Maxim.
 * It reports up to two temperatures (its own plus up to
 * one external one).
 */


#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/jiffies.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/err.h>
#include <linux/sysfs.h>
#include <linux/platform_device.h>
#include <linux/mfd/max17135.h>
#include <linux/mod_devicetable.h>

/*
 * Conversions
 */
static int temp_from_reg(int val)
{
	return val >> 8;
}

/*
 * Functions declaration
 */
static int max17135_sensor_probe(struct platform_device *pdev);
static int max17135_sensor_remove(struct platform_device *pdev);

static const struct platform_device_id max17135_sns_id[] = {
	{ "max17135-sns", 0},
	{ /* sentinel */ },
};
MODULE_DEVICE_TABLE(platform, max17135_sns_id);

/*
 * Driver data (common to all clients)
 */
static struct platform_driver max17135_sensor_driver = {
	.probe = max17135_sensor_probe,
	.remove = max17135_sensor_remove,
	.id_table = max17135_sns_id,
	.driver = {
		.name = "max17135_sensor",
	},
};

/*
 * Client data (each client gets its own)
 */
struct max17135_data {
	struct device *hwmon_dev;
};

/*
 * Sysfs stuff
 */
static ssize_t show_temp_input1(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	unsigned int reg_val;
	max17135_reg_read(REG_MAX17135_INT_TEMP, &reg_val);
	return snprintf(buf, PAGE_SIZE, "%d\n", temp_from_reg(reg_val));
}

static ssize_t show_temp_input2(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	unsigned int reg_val;
	max17135_reg_read(REG_MAX17135_EXT_TEMP, &reg_val);
	return snprintf(buf, PAGE_SIZE, "%d\n", temp_from_reg(reg_val));
}

static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
static DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input2, NULL);

static struct attribute *max17135_attributes[] = {
	&dev_attr_temp1_input.attr,
	&dev_attr_temp2_input.attr,
	NULL
};

static const struct attribute_group max17135_group = {
	.attrs = max17135_attributes,
};

/*
 * Real code
 */
static int max17135_sensor_probe(struct platform_device *pdev)
{
	struct max17135_data *data;
	int err;

	data = kzalloc(sizeof(struct max17135_data), GFP_KERNEL);
	if (!data) {
		err = -ENOMEM;
		goto exit;
	}

	/* Register sysfs hooks */
	err = sysfs_create_group(&pdev->dev.kobj, &max17135_group);
	if (err)
		goto exit_free;

	data->hwmon_dev = hwmon_device_register(&pdev->dev);
	if (IS_ERR(data->hwmon_dev)) {
		err = PTR_ERR(data->hwmon_dev);
		goto exit_remove_files;
	}

	platform_set_drvdata(pdev, data);

	return 0;

exit_remove_files:
	sysfs_remove_group(&pdev->dev.kobj, &max17135_group);
exit_free:
	kfree(data);
exit:
	return err;
}

static int max17135_sensor_remove(struct platform_device *pdev)
{
	struct max17135_data *data = platform_get_drvdata(pdev);

	hwmon_device_unregister(data->hwmon_dev);
	sysfs_remove_group(&pdev->dev.kobj, &max17135_group);

	kfree(data);
	return 0;
}

static int __init sensors_max17135_init(void)
{
	return platform_driver_register(&max17135_sensor_driver);
}
module_init(sensors_max17135_init);

static void __exit sensors_max17135_exit(void)
{
	platform_driver_unregister(&max17135_sensor_driver);
}
module_exit(sensors_max17135_exit);

MODULE_DESCRIPTION("MAX17135 sensor driver");
MODULE_LICENSE("GPL");