1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2022-2024, Linaro Ltd
8 #include <linux/auxiliary_bus.h>
9 #include <linux/cleanup.h>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/i2c.h>
13 #include <linux/irqreturn.h>
14 #include <linux/lockdep.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/notifier.h>
18 #include <linux/slab.h>
19 #include <linux/platform_data/lenovo-yoga-c630.h>
21 #define LENOVO_EC_RESPONSE_REG 0x01
22 #define LENOVO_EC_REQUEST_REG 0x02
24 #define LENOVO_EC_UCSI_WRITE 0x20
25 #define LENOVO_EC_UCSI_READ 0x21
27 #define LENOVO_EC_READ_REG 0xb0
28 #define LENOVO_EC_REQUEST_NEXT_EVENT 0x84
30 #define LENOVO_EC_UCSI_VERSION 0x20
33 struct i2c_client
*client
;
35 struct blocking_notifier_head notifier_list
;
38 static int yoga_c630_ec_request(struct yoga_c630_ec
*ec
, u8
*req
, size_t req_len
,
39 u8
*resp
, size_t resp_len
)
43 lockdep_assert_held(&ec
->lock
);
45 ret
= i2c_smbus_write_i2c_block_data(ec
->client
, LENOVO_EC_REQUEST_REG
,
50 return i2c_smbus_read_i2c_block_data(ec
->client
, LENOVO_EC_RESPONSE_REG
,
54 int yoga_c630_ec_read8(struct yoga_c630_ec
*ec
, u8 addr
)
56 u8 req
[2] = { LENOVO_EC_READ_REG
, };
60 guard(mutex
)(&ec
->lock
);
63 ret
= yoga_c630_ec_request(ec
, req
, sizeof(req
), &val
, 1);
69 EXPORT_SYMBOL_GPL(yoga_c630_ec_read8
);
71 int yoga_c630_ec_read16(struct yoga_c630_ec
*ec
, u8 addr
)
73 u8 req
[2] = { LENOVO_EC_READ_REG
, };
78 /* don't overflow the address */
82 guard(mutex
)(&ec
->lock
);
85 ret
= yoga_c630_ec_request(ec
, req
, sizeof(req
), &lsb
, 1);
90 ret
= yoga_c630_ec_request(ec
, req
, sizeof(req
), &msb
, 1);
94 return msb
<< 8 | lsb
;
96 EXPORT_SYMBOL_GPL(yoga_c630_ec_read16
);
98 u16
yoga_c630_ec_ucsi_get_version(struct yoga_c630_ec
*ec
)
100 u8 req
[3] = { 0xb3, 0xf2, };
105 guard(mutex
)(&ec
->lock
);
107 req
[2] = LENOVO_EC_UCSI_VERSION
;
108 ret
= yoga_c630_ec_request(ec
, req
, sizeof(req
), &lsb
, 1);
112 req
[2] = LENOVO_EC_UCSI_VERSION
+ 1;
113 ret
= yoga_c630_ec_request(ec
, req
, sizeof(req
), &msb
, 1);
117 return msb
<< 8 | lsb
;
119 EXPORT_SYMBOL_GPL(yoga_c630_ec_ucsi_get_version
);
121 int yoga_c630_ec_ucsi_write(struct yoga_c630_ec
*ec
,
122 const u8 req
[YOGA_C630_UCSI_WRITE_SIZE
])
126 mutex_lock(&ec
->lock
);
127 ret
= i2c_smbus_write_i2c_block_data(ec
->client
, LENOVO_EC_UCSI_WRITE
,
128 YOGA_C630_UCSI_WRITE_SIZE
, req
);
129 mutex_unlock(&ec
->lock
);
131 return ret
< 0 ? ret
: 0;
133 EXPORT_SYMBOL_GPL(yoga_c630_ec_ucsi_write
);
135 int yoga_c630_ec_ucsi_read(struct yoga_c630_ec
*ec
,
136 u8 resp
[YOGA_C630_UCSI_READ_SIZE
])
140 mutex_lock(&ec
->lock
);
141 ret
= i2c_smbus_read_i2c_block_data(ec
->client
, LENOVO_EC_UCSI_READ
,
142 YOGA_C630_UCSI_READ_SIZE
, resp
);
143 mutex_unlock(&ec
->lock
);
145 return ret
< 0 ? ret
: 0;
147 EXPORT_SYMBOL_GPL(yoga_c630_ec_ucsi_read
);
149 static irqreturn_t
yoga_c630_ec_thread_intr(int irq
, void *data
)
151 u8 req
[] = { LENOVO_EC_REQUEST_NEXT_EVENT
};
152 struct yoga_c630_ec
*ec
= data
;
156 mutex_lock(&ec
->lock
);
157 ret
= yoga_c630_ec_request(ec
, req
, sizeof(req
), &event
, 1);
158 mutex_unlock(&ec
->lock
);
162 blocking_notifier_call_chain(&ec
->notifier_list
, event
, ec
);
168 * yoga_c630_ec_register_notify - Register a notifier callback for EC events.
170 * @nb: Notifier block pointer to register
172 * Return: 0 on success or negative error code.
174 int yoga_c630_ec_register_notify(struct yoga_c630_ec
*ec
, struct notifier_block
*nb
)
176 return blocking_notifier_chain_register(&ec
->notifier_list
, nb
);
178 EXPORT_SYMBOL_GPL(yoga_c630_ec_register_notify
);
181 * yoga_c630_ec_unregister_notify - Unregister notifier callback for EC events.
183 * @nb: Notifier block pointer to unregister
185 * Unregister a notifier callback that was previously registered with
186 * yoga_c630_ec_register_notify().
188 void yoga_c630_ec_unregister_notify(struct yoga_c630_ec
*ec
, struct notifier_block
*nb
)
190 blocking_notifier_chain_unregister(&ec
->notifier_list
, nb
);
192 EXPORT_SYMBOL_GPL(yoga_c630_ec_unregister_notify
);
194 static void yoga_c630_aux_release(struct device
*dev
)
196 struct auxiliary_device
*adev
= to_auxiliary_dev(dev
);
201 static void yoga_c630_aux_remove(void *data
)
203 struct auxiliary_device
*adev
= data
;
205 auxiliary_device_delete(adev
);
206 auxiliary_device_uninit(adev
);
209 static int yoga_c630_aux_init(struct device
*parent
, const char *name
,
210 struct yoga_c630_ec
*ec
)
212 struct auxiliary_device
*adev
;
215 adev
= kzalloc(sizeof(*adev
), GFP_KERNEL
);
221 adev
->dev
.parent
= parent
;
222 adev
->dev
.release
= yoga_c630_aux_release
;
223 adev
->dev
.platform_data
= ec
;
225 ret
= auxiliary_device_init(adev
);
231 ret
= auxiliary_device_add(adev
);
233 auxiliary_device_uninit(adev
);
237 return devm_add_action_or_reset(parent
, yoga_c630_aux_remove
, adev
);
240 static int yoga_c630_ec_probe(struct i2c_client
*client
)
242 struct device
*dev
= &client
->dev
;
243 struct yoga_c630_ec
*ec
;
246 ec
= devm_kzalloc(dev
, sizeof(*ec
), GFP_KERNEL
);
250 mutex_init(&ec
->lock
);
252 BLOCKING_INIT_NOTIFIER_HEAD(&ec
->notifier_list
);
254 ret
= devm_request_threaded_irq(dev
, client
->irq
,
255 NULL
, yoga_c630_ec_thread_intr
,
256 IRQF_ONESHOT
, "yoga_c630_ec", ec
);
258 return dev_err_probe(dev
, ret
, "unable to request irq\n");
260 ret
= yoga_c630_aux_init(dev
, YOGA_C630_DEV_PSY
, ec
);
264 return yoga_c630_aux_init(dev
, YOGA_C630_DEV_UCSI
, ec
);
268 static const struct of_device_id yoga_c630_ec_of_match
[] = {
269 { .compatible
= "lenovo,yoga-c630-ec" },
272 MODULE_DEVICE_TABLE(of
, yoga_c630_ec_of_match
);
274 static const struct i2c_device_id yoga_c630_ec_i2c_id_table
[] = {
278 MODULE_DEVICE_TABLE(i2c
, yoga_c630_ec_i2c_id_table
);
280 static struct i2c_driver yoga_c630_ec_i2c_driver
= {
282 .name
= "yoga-c630-ec",
283 .of_match_table
= yoga_c630_ec_of_match
285 .probe
= yoga_c630_ec_probe
,
286 .id_table
= yoga_c630_ec_i2c_id_table
,
288 module_i2c_driver(yoga_c630_ec_i2c_driver
);
290 MODULE_DESCRIPTION("Lenovo Yoga C630 Embedded Controller");
291 MODULE_LICENSE("GPL");