首页 > 系统相关 >按键 芯片型号max7359 linux驱动程序

按键 芯片型号max7359 linux驱动程序

时间:2024-12-01 11:32:32浏览次数:13  
标签:驱动程序 keypad linux dev client MAX7359 max7359 input

/*

  • max7359_keypad.c - MAX7359 Key Switch Controller Driver
  • Copyright © 2009 Samsung Electronics
  • Based on pxa27x_keypad.c
  • This program is free software; you can redistribute it and/or modify
  • it under the terms of the GNU General Public License version 2 as
  • published by the Free Software Foundation.
  • Datasheet: http://www.maxim-ic.com/quick_view2.cfm/qv_pk/5456
    */

#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/pm.h>
#include <linux/input.h>
#include <linux/input/matrix_keypad.h>

#define MAX7359_MAX_KEY_ROWS 8
#define MAX7359_MAX_KEY_COLS 8
#define MAX7359_MAX_KEY_NUM (MAX7359_MAX_KEY_ROWS * MAX7359_MAX_KEY_COLS)
#define MAX7359_ROW_SHIFT 3

/*

  • MAX7359 registers
    */
    #define MAX7359_REG_KEYFIFO 0x00
    #define MAX7359_REG_CONFIG 0x01
    #define MAX7359_REG_DEBOUNCE 0x02
    #define MAX7359_REG_INTERRUPT 0x03
    #define MAX7359_REG_PORTS 0x04
    #define MAX7359_REG_KEYREP 0x05
    #define MAX7359_REG_SLEEP 0x06

/*

  • Configuration register bits
    */
    #define MAX7359_CFG_SLEEP (1 << 7)
    #define MAX7359_CFG_INTERRUPT (1 << 5)
    #define MAX7359_CFG_KEY_RELEASE (1 << 3)
    #define MAX7359_CFG_WAKEUP (1 << 1)
    #define MAX7359_CFG_TIMEOUT (1 << 0)

/*

  • Autosleep register values (ms)
    */
    #define MAX7359_AUTOSLEEP_8192 0x01
    #define MAX7359_AUTOSLEEP_4096 0x02
    #define MAX7359_AUTOSLEEP_2048 0x03
    #define MAX7359_AUTOSLEEP_1024 0x04
    #define MAX7359_AUTOSLEEP_512 0x05
    #define MAX7359_AUTOSLEEP_256 0x06

struct max7359_keypad {
/* matrix key code map */
unsigned short keycodes[MAX7359_MAX_KEY_NUM];

struct input_dev *input_dev;
struct i2c_client *client;

};

static int max7359_write_reg(struct i2c_client *client, u8 reg, u8 val)
{
int ret = i2c_smbus_write_byte_data(client, reg, val);

if (ret < 0)
	dev_err(&client->dev, "%s: reg 0x%x, val 0x%x, err %d\n",
		__func__, reg, val, ret);
return ret;

}

static int max7359_read_reg(struct i2c_client *client, int reg)
{
int ret = i2c_smbus_read_byte_data(client, reg);

if (ret < 0)
	dev_err(&client->dev, "%s: reg 0x%x, err %d\n",
		__func__, reg, ret);
return ret;

}

/* runs in an IRQ thread – can (and will!) sleep */
static irqreturn_t max7359_interrupt(int irq, void *dev_id)
{
struct max7359_keypad *keypad = dev_id;
struct input_dev *input_dev = keypad->input_dev;
int val, row, col, release, code;

val = max7359_read_reg(keypad->client, MAX7359_REG_KEYFIFO);
row = val & 0x7;
col = (val >> 3) & 0x7;
release = val & 0x40;

code = MATRIX_SCAN_CODE(row, col, MAX7359_ROW_SHIFT);

dev_dbg(&keypad->client->dev,
	"key[%d:%d] %s\n", row, col, release ? "release" : "press");

input_event(input_dev, EV_MSC, MSC_SCAN, code);
input_report_key(input_dev, keypad->keycodes[code], !release);
input_sync(input_dev);

return IRQ_HANDLED;

}

/*

  • Let MAX7359 fall into a deep sleep:
  • If no keys are pressed, enter sleep mode for 8192 ms. And if any
  • key is pressed, the MAX7359 returns to normal operating mode.
    */
    static inline void max7359_fall_deepsleep(struct i2c_client *client)
    {
    max7359_write_reg(client, MAX7359_REG_SLEEP, MAX7359_AUTOSLEEP_8192);
    }

/*

  • Let MAX7359 take a catnap:
  • Autosleep just for 256 ms.
    */
    static inline void max7359_take_catnap(struct i2c_client *client)
    {
    max7359_write_reg(client, MAX7359_REG_SLEEP, MAX7359_AUTOSLEEP_256);
    }

static int max7359_open(struct input_dev *dev)
{
struct max7359_keypad *keypad = input_get_drvdata(dev);

max7359_take_catnap(keypad->client);

return 0;

}

static void max7359_close(struct input_dev *dev)
{
struct max7359_keypad *keypad = input_get_drvdata(dev);

max7359_fall_deepsleep(keypad->client);

}

static void max7359_initialize(struct i2c_client client)
{
max7359_write_reg(client, MAX7359_REG_CONFIG,
MAX7359_CFG_KEY_RELEASE | /
Key release enable /
MAX7359_CFG_WAKEUP); /
Key press wakeup enable */

/* Full key-scan functionality */
max7359_write_reg(client, MAX7359_REG_DEBOUNCE, 0x1F);

/* nINT asserts every debounce cycles */
max7359_write_reg(client, MAX7359_REG_INTERRUPT, 0x01);

max7359_fall_deepsleep(client);

}

static int max7359_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
const struct matrix_keymap_data *keymap_data =
dev_get_platdata(&client->dev);
struct max7359_keypad *keypad;
struct input_dev *input_dev;
int ret;
int error;

if (!client->irq) {
	dev_err(&client->dev, "The irq number should not be zero\n");
	return -EINVAL;
}

/* Detect MAX7359: The initial Keys FIFO value is '0x3F' */
ret = max7359_read_reg(client, MAX7359_REG_KEYFIFO);
if (ret < 0) {
	dev_err(&client->dev, "failed to detect device\n");
	return -ENODEV;
}

dev_dbg(&client->dev, "keys FIFO is 0x%02x\n", ret);

keypad = devm_kzalloc(&client->dev, sizeof(struct max7359_keypad),
		      GFP_KERNEL);
if (!keypad) {
	dev_err(&client->dev, "failed to allocate memory\n");
	return -ENOMEM;
}

input_dev = devm_input_allocate_device(&client->dev);
if (!input_dev) {
	dev_err(&client->dev, "failed to allocate input device\n");
	return -ENOMEM;
}

keypad->client = client;
keypad->input_dev = input_dev;

input_dev->name = client->name;
input_dev->id.bustype = BUS_I2C;
input_dev->open = max7359_open;
input_dev->close = max7359_close;
input_dev->dev.parent = &client->dev;

input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
input_dev->keycodesize = sizeof(keypad->keycodes[0]);
input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes);
input_dev->keycode = keypad->keycodes;

input_set_capability(input_dev, EV_MSC, MSC_SCAN);
input_set_drvdata(input_dev, keypad);

error = matrix_keypad_build_keymap(keymap_data, NULL,
				   MAX7359_MAX_KEY_ROWS,
				   MAX7359_MAX_KEY_COLS,
				   keypad->keycodes,
				   input_dev);
if (error) {
	dev_err(&client->dev, "failed to build keymap\n");
	return error;
}

error = devm_request_threaded_irq(&client->dev, client->irq, NULL,
				  max7359_interrupt,
				  IRQF_TRIGGER_LOW | IRQF_ONESHOT,
				  client->name, keypad);
if (error) {
	dev_err(&client->dev, "failed to register interrupt\n");
	return error;
}

/* Register the input device */
error = input_register_device(input_dev);
if (error) {
	dev_err(&client->dev, "failed to register input device\n");
	return error;
}

/* Initialize MAX7359 */
max7359_initialize(client);

device_init_wakeup(&client->dev, 1);

return 0;

}

#ifdef CONFIG_PM_SLEEP
static int max7359_suspend(struct device *dev)
{
struct i2c_client *client = to_i2c_client(dev);

max7359_fall_deepsleep(client);

if (device_may_wakeup(&client->dev))
	enable_irq_wake(client->irq);

return 0;

}

static int max7359_resume(struct device *dev)
{
struct i2c_client *client = to_i2c_client(dev);

if (device_may_wakeup(&client->dev))
	disable_irq_wake(client->irq);

/* Restore the default setting */
max7359_take_catnap(client);

return 0;

}
#endif

static SIMPLE_DEV_PM_OPS(max7359_pm, max7359_suspend, max7359_resume);

static const struct i2c_device_id max7359_ids[] = {
{ “max7359”, 0 },
{ }
};
MODULE_DEVICE_TABLE(i2c, max7359_ids);

static struct i2c_driver max7359_i2c_driver = {
.driver = {
.name = “max7359”,
.pm = &max7359_pm,
},
.probe = max7359_probe,
.id_table = max7359_ids,
};

module_i2c_driver(max7359_i2c_driver);

MODULE_AUTHOR(“xxx”);
MODULE_DESCRIPTION(“MAX7359 Key Switch Controller Driver”);
MODULE_LICENSE(“GPL v2”);

标签:驱动程序,keypad,linux,dev,client,MAX7359,max7359,input
From: https://blog.csdn.net/baidu_37552881/article/details/143961072

相关文章

  • 按键 芯片jornada680 linux驱动程序
    /*drivers/input/keyboard/jornada680_kbd.cHPJornada620/660/680/690scankeyboardplatformdriverBasedonhp680_keyb.cCopyright©2006PaulMundtCopyright©2005AndriySkulyshSplitfromdrivers/input/keyboard/hp600_keyb.cCopyright©2000YaegashiT......
  • Linux提权之八大实战利器与高权限操作技巧
    文章目录LINUX提权0x01.SUID提权chmod命令Find命令(查找有SUID权限的文件)find提权nmap提权vim提权Bash提权Less或More提权nano提权CP提权awk提权0x02sudo提权0x03计划任务提权0x04内核漏洞提权0x05环境变量劫持提权0x06NFS提权0x07通配符WS提权0x08明文密码提权0x9lx......
  • Linux操作系统2-进程控制3(进程替换,exec相关函数和系统调用)
    上篇文章:Linux操作系统2-进程控制2(进程等待,waitpid系统调用,阻塞与非阻塞等待)-CSDN博客本篇代码Gitee仓库:Linux操作系统-进程的程序替换学习·d0f7bb4·橘子真甜/linux学习-Gitee.com本篇重点:进程替换目录一.什么是进程替换?二.进程替换函数常用的函数 2.1......
  • 【Linux】shell编程基础
    一、脚本的创建和执行创建一个1.sh文件vim1.sh在文件中写入以下内容:#!/bin/bash#或者写入#!/bin/sh#或者写入#!/bin/dash无论使用哪种脚本解释器,最终调用的可能是dash。检查默认的sh解释器:ls-l/bin/sh执行脚本文件./1.sh如果权限不足,可以......
  • Linux的150个常用命令汇总,运维大神不一定全部掌握!零基础入门到精通,收藏这一篇就够了
    下面是分类总结的150个命令,看一下你掌握了多少个?1文件和目录操作命令命令作用pwd显示当前所在位置cd切换目录tree以树形结构显示目录下的内容mkdir创建目录touch创建空文件ls显示目录下的内容及相关信息属性cp复制文件或目录mv移动或重命名文件rm删除文件或目录ln创建硬......
  • linux安装intel编译器2018
    加压软件/public/download/parallel_studio_2018.tgz进入目录后用./install.sh开始安装 按回车 这个选择1,然后需要输入lic激活文件路径。 安装完成后添加PATH到环境变量中1、如果是普通用户,在用户的.bashrc里面添加2、如果是root用户,在/etc/profile文件的最......
  • [Ubuntu] linux之Ubuntu18.04的下载及在虚拟机中详细安装过程(附有下载链接)
    前言ubuntu链接:https://pan.quark.cn/s/283509d0d36e提取码:dfT1链接失效(可能被官方和谐)可评论或私信我重发下载压缩包后解压!!安装路径不要有中文下载后解压得到.iso文件,不要放在有中文路径的目录下,我这里是解压放在E盘下打开虚拟机,这里使用的是VMwareWorkstation......
  • 学霸带你游戏化 Linux 防火墙 iptables、firewalld
    网络安全与流量控制的重要性在当今数字化时代,网络安全成为用户和组织关注的核心问题。对于在线游戏而言,稳定的网络连接和安全的环境是保证良好体验的基础。无论是《绝地求生》(PUBG)中的激烈战斗,还是《英雄联盟》(LeagueofLegends)的团队协作,都离不开高效的网络流量管理与安全防......
  • Linux程序员解决程序崩溃的问题
    Linux程序员解决程序崩溃的问题1.引言嘿,各位程序员小伙伴们!你们是否曾经遇到过程序突然“跑路”崩溃的情况?是不是觉得那一刻就像被一只无形的手拍在了脑门上,整个人都懵了?别担心,今天我们就来聊聊如何像侦探一样追查程序崩溃的真相,让你的代码更加坚不可摧!2.程序崩溃的常......
  • [Linux]动静态库
    动静态库静态库(.a):程序在编译链接的时候把库的代码链接到可执行文件中。程序运行的时候将不再需要静态库,但是,多个程序使用相同的静态库时,每个程序都会包含一份库的代码,可能会导致可执行文件体积较大。动态库(.so):是在程序运行时被加载的库。当一个程序链接了动态库,在程序启动时,操......