Skip to content

Commit eceae58

Browse files
neuschaeferLee Jones
authored andcommitted
mfd: Add base driver for Netronix embedded controller
The Netronix embedded controller is a microcontroller found in some e-book readers designed by the original design manufacturer Netronix, Inc. It contains RTC, battery monitoring, system power management, and PWM functionality. This driver implements register access and version detection. Third-party hardware documentation is available at: https://github.com/neuschaefer/linux/wiki/Netronix-MSP430-embedded-controller The EC supports interrupts, but the driver doesn't make use of them so far. Signed-off-by: Jonathan Neuschäfer <j.neuschaefer@gmx.net> Signed-off-by: Lee Jones <lee.jones@linaro.org>
1 parent 01929c7 commit eceae58

4 files changed

Lines changed: 270 additions & 0 deletions

File tree

‎drivers/mfd/Kconfig‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,17 @@ config MFD_VIPERBOARD
967967
You need to select the mfd cell drivers separately.
968968
The drivers do not support all features the board exposes.
969969

970+
config MFD_NTXEC
971+
tristate "Netronix embedded controller (EC)"
972+
depends on OF || COMPILE_TEST
973+
depends on I2C
974+
select REGMAP_I2C
975+
select MFD_CORE
976+
help
977+
Say yes here if you want to support the embedded controller found in
978+
certain e-book readers designed by the original design manufacturer
979+
Netronix.
980+
970981
config MFD_RETU
971982
tristate "Nokia Retu and Tahvo multi-function device"
972983
select MFD_CORE

‎drivers/mfd/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ obj-$(CONFIG_MFD_INTEL_PMC_BXT) += intel_pmc_bxt.o
218218
obj-$(CONFIG_MFD_INTEL_PMT) += intel_pmt.o
219219
obj-$(CONFIG_MFD_PALMAS) += palmas.o
220220
obj-$(CONFIG_MFD_VIPERBOARD) += viperboard.o
221+
obj-$(CONFIG_MFD_NTXEC) += ntxec.o
221222
obj-$(CONFIG_MFD_RC5T583) += rc5t583.o rc5t583-irq.o
222223
obj-$(CONFIG_MFD_RK808) += rk808.o
223224
obj-$(CONFIG_MFD_RN5T618) += rn5t618.o

‎drivers/mfd/ntxec.c‎

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* The Netronix embedded controller is a microcontroller found in some
4+
* e-book readers designed by the original design manufacturer Netronix, Inc.
5+
* It contains RTC, battery monitoring, system power management, and PWM
6+
* functionality.
7+
*
8+
* This driver implements register access, version detection, and system
9+
* power-off/reset.
10+
*
11+
* Copyright 2020 Jonathan Neuschäfer <j.neuschaefer@gmx.net>
12+
*/
13+
14+
#include <linux/delay.h>
15+
#include <linux/errno.h>
16+
#include <linux/i2c.h>
17+
#include <linux/mfd/core.h>
18+
#include <linux/mfd/ntxec.h>
19+
#include <linux/module.h>
20+
#include <linux/pm.h>
21+
#include <linux/reboot.h>
22+
#include <linux/regmap.h>
23+
#include <linux/types.h>
24+
#include <asm/unaligned.h>
25+
26+
#define NTXEC_REG_VERSION 0x00
27+
#define NTXEC_REG_POWEROFF 0x50
28+
#define NTXEC_REG_POWERKEEP 0x70
29+
#define NTXEC_REG_RESET 0x90
30+
31+
#define NTXEC_POWEROFF_VALUE 0x0100
32+
#define NTXEC_POWERKEEP_VALUE 0x0800
33+
#define NTXEC_RESET_VALUE 0xff00
34+
35+
static struct i2c_client *poweroff_restart_client;
36+
37+
static void ntxec_poweroff(void)
38+
{
39+
int res;
40+
u8 buf[3] = { NTXEC_REG_POWEROFF };
41+
struct i2c_msg msgs[] = {
42+
{
43+
.addr = poweroff_restart_client->addr,
44+
.flags = 0,
45+
.len = sizeof(buf),
46+
.buf = buf,
47+
},
48+
};
49+
50+
put_unaligned_be16(NTXEC_POWEROFF_VALUE, buf + 1);
51+
52+
res = i2c_transfer(poweroff_restart_client->adapter, msgs, ARRAY_SIZE(msgs));
53+
if (res < 0)
54+
dev_warn(&poweroff_restart_client->dev,
55+
"Failed to power off (err = %d)\n", res);
56+
57+
/*
58+
* The time from the register write until the host CPU is powered off
59+
* has been observed to be about 2.5 to 3 seconds. Sleep long enough to
60+
* safely avoid returning from the poweroff handler.
61+
*/
62+
msleep(5000);
63+
}
64+
65+
static int ntxec_restart(struct notifier_block *nb,
66+
unsigned long action, void *data)
67+
{
68+
int res;
69+
u8 buf[3] = { NTXEC_REG_RESET };
70+
/*
71+
* NOTE: The lower half of the reset value is not sent, because sending
72+
* it causes an I2C error. (The reset handler in the downstream driver
73+
* does send the full two-byte value, but doesn't check the result).
74+
*/
75+
struct i2c_msg msgs[] = {
76+
{
77+
.addr = poweroff_restart_client->addr,
78+
.flags = 0,
79+
.len = sizeof(buf) - 1,
80+
.buf = buf,
81+
},
82+
};
83+
84+
put_unaligned_be16(NTXEC_RESET_VALUE, buf + 1);
85+
86+
res = i2c_transfer(poweroff_restart_client->adapter, msgs, ARRAY_SIZE(msgs));
87+
if (res < 0)
88+
dev_warn(&poweroff_restart_client->dev,
89+
"Failed to restart (err = %d)\n", res);
90+
91+
return NOTIFY_DONE;
92+
}
93+
94+
static struct notifier_block ntxec_restart_handler = {
95+
.notifier_call = ntxec_restart,
96+
.priority = 128,
97+
};
98+
99+
static const struct regmap_config regmap_config = {
100+
.name = "ntxec",
101+
.reg_bits = 8,
102+
.val_bits = 16,
103+
.cache_type = REGCACHE_NONE,
104+
.val_format_endian = REGMAP_ENDIAN_BIG,
105+
};
106+
107+
static const struct mfd_cell ntxec_subdevices[] = {
108+
{ .name = "ntxec-rtc" },
109+
{ .name = "ntxec-pwm" },
110+
};
111+
112+
static int ntxec_probe(struct i2c_client *client)
113+
{
114+
struct ntxec *ec;
115+
unsigned int version;
116+
int res;
117+
118+
ec = devm_kmalloc(&client->dev, sizeof(*ec), GFP_KERNEL);
119+
if (!ec)
120+
return -ENOMEM;
121+
122+
ec->dev = &client->dev;
123+
124+
ec->regmap = devm_regmap_init_i2c(client, &regmap_config);
125+
if (IS_ERR(ec->regmap)) {
126+
dev_err(ec->dev, "Failed to set up regmap for device\n");
127+
return PTR_ERR(ec->regmap);
128+
}
129+
130+
/* Determine the firmware version */
131+
res = regmap_read(ec->regmap, NTXEC_REG_VERSION, &version);
132+
if (res < 0) {
133+
dev_err(ec->dev, "Failed to read firmware version number\n");
134+
return res;
135+
}
136+
137+
/* Bail out if we encounter an unknown firmware version */
138+
switch (version) {
139+
case NTXEC_VERSION_KOBO_AURA:
140+
break;
141+
default:
142+
dev_err(ec->dev,
143+
"Netronix embedded controller version %04x is not supported.\n",
144+
version);
145+
return -ENODEV;
146+
}
147+
148+
dev_info(ec->dev,
149+
"Netronix embedded controller version %04x detected.\n", version);
150+
151+
if (of_device_is_system_power_controller(ec->dev->of_node)) {
152+
/*
153+
* Set the 'powerkeep' bit. This is necessary on some boards
154+
* in order to keep the system running.
155+
*/
156+
res = regmap_write(ec->regmap, NTXEC_REG_POWERKEEP,
157+
NTXEC_POWERKEEP_VALUE);
158+
if (res < 0)
159+
return res;
160+
161+
if (poweroff_restart_client)
162+
/*
163+
* Another instance of the driver already took
164+
* poweroff/restart duties.
165+
*/
166+
dev_err(ec->dev, "poweroff_restart_client already assigned\n");
167+
else
168+
poweroff_restart_client = client;
169+
170+
if (pm_power_off)
171+
/* Another driver already registered a poweroff handler. */
172+
dev_err(ec->dev, "pm_power_off already assigned\n");
173+
else
174+
pm_power_off = ntxec_poweroff;
175+
176+
res = register_restart_handler(&ntxec_restart_handler);
177+
if (res)
178+
dev_err(ec->dev,
179+
"Failed to register restart handler: %d\n", res);
180+
}
181+
182+
i2c_set_clientdata(client, ec);
183+
184+
res = devm_mfd_add_devices(ec->dev, PLATFORM_DEVID_NONE, ntxec_subdevices,
185+
ARRAY_SIZE(ntxec_subdevices), NULL, 0, NULL);
186+
if (res)
187+
dev_err(ec->dev, "Failed to add subdevices: %d\n", res);
188+
189+
return res;
190+
}
191+
192+
static int ntxec_remove(struct i2c_client *client)
193+
{
194+
if (client == poweroff_restart_client) {
195+
poweroff_restart_client = NULL;
196+
pm_power_off = NULL;
197+
unregister_restart_handler(&ntxec_restart_handler);
198+
}
199+
200+
return 0;
201+
}
202+
203+
static const struct of_device_id of_ntxec_match_table[] = {
204+
{ .compatible = "netronix,ntxec", },
205+
{}
206+
};
207+
MODULE_DEVICE_TABLE(of, of_ntxec_match_table);
208+
209+
static struct i2c_driver ntxec_driver = {
210+
.driver = {
211+
.name = "ntxec",
212+
.of_match_table = of_ntxec_match_table,
213+
},
214+
.probe_new = ntxec_probe,
215+
.remove = ntxec_remove,
216+
};
217+
module_i2c_driver(ntxec_driver);
218+
219+
MODULE_AUTHOR("Jonathan Neuschäfer <j.neuschaefer@gmx.net>");
220+
MODULE_DESCRIPTION("Core driver for Netronix EC");
221+
MODULE_LICENSE("GPL");

‎include/linux/mfd/ntxec.h‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
/*
3+
* Copyright 2020 Jonathan Neuschäfer
4+
*
5+
* Register access and version information for the Netronix embedded
6+
* controller.
7+
*/
8+
9+
#ifndef NTXEC_H
10+
#define NTXEC_H
11+
12+
#include <linux/types.h>
13+
14+
struct device;
15+
struct regmap;
16+
17+
struct ntxec {
18+
struct device *dev;
19+
struct regmap *regmap;
20+
};
21+
22+
/*
23+
* Some registers, such as the battery status register (0x41), are in
24+
* big-endian, but others only have eight significant bits, which are in the
25+
* first byte transmitted over I2C (the MSB of the big-endian value).
26+
* This convenience function converts an 8-bit value to 16-bit for use in the
27+
* second kind of register.
28+
*/
29+
static inline __be16 ntxec_reg8(u8 value)
30+
{
31+
return value << 8;
32+
}
33+
34+
/* Known firmware versions */
35+
#define NTXEC_VERSION_KOBO_AURA 0xd726 /* found in Kobo Aura */
36+
37+
#endif

0 commit comments

Comments
 (0)