Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tpetazzoni/c6abeb707c2ed86ae7efe03232f97e14 to your computer and use it in GitHub Desktop.
Save tpetazzoni/c6abeb707c2ed86ae7efe03232f97e14 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: GPL-2.0
#include <linux/init.h>
#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/delay.h>
static void nunchuk_read_regs(struct i2c_client *client,
u8 *buf)
{
char zero = 0;
int ret;
msleep(10);
ret = i2c_master_send(client, &zero, sizeof(zero));
if (ret != sizeof(zero))
return;
msleep(10);
ret = i2c_master_recv(client, buf, 6);
if (ret != 6)
return;
}
static int nunchuk_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
char buf1[] = { 0xf0, 0x55 };
char buf2[] = { 0xfb, 0x00 };
char regs[6];
int ret, zpressed, cpressed;
pr_info("Coucou from %s\n", __func__);
ret = i2c_master_send(client, buf1, sizeof(buf1));
if (ret != sizeof(buf1))
return -ENODEV;
udelay(1000);
ret = i2c_master_send(client, buf2, sizeof(buf2));
if (ret != sizeof(buf2))
return -ENODEV;
/* dummy read */
nunchuk_read_regs(client, regs);
nunchuk_read_regs(client, regs);
zpressed = regs[5] & BIT(0) ? 0 : 1;
cpressed = regs[5] & BIT(1) ? 0 : 1;
pr_info("zpressed: %s, cpressed = %s\n",
zpressed ? "YES" : "NO",
cpressed ? "YES" : "NO");
return 0;
}
static int nunchuk_remove(struct i2c_client *client)
{
pr_info("Coucou from %s\n", __func__);
return 0;
}
static const struct of_device_id nunchuk_of_ids[] = {
{ .compatible = "nintendo,nunchuk" },
{ /* sentinel */ },
};
static struct i2c_driver nunchuk_driver = {
.probe = nunchuk_probe,
.remove = nunchuk_remove,
.driver = {
.name = "nunchuk",
.of_match_table = nunchuk_of_ids,
},
};
module_i2c_driver(nunchuk_driver);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Thomas Petazzoni");
MODULE_DESCRIPTION("Nunchuk driver");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment