/*
* DRV2667 haptics driver family
*
* Author: Dan Murphy <[email protected]>
*
* Copyright: (C) 2014 Texas Instruments, Inc.
*
* 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.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*/
#include <linux/i2c.h>
#include <linux/input.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/regmap.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/regulator/consumer.h>
/* Contol registers */
#define DRV2667_STATUS 0x00
#define DRV2667_CTRL_1 0x01
#define DRV2667_CTRL_2 0x02
/* Waveform sequencer */
#define DRV2667_WV_SEQ_0 0x03
#define DRV2667_WV_SEQ_1 0x04
#define DRV2667_WV_SEQ_2 0x05
#define DRV2667_WV_SEQ_3 0x06
#define DRV2667_WV_SEQ_4 0x07
#define DRV2667_WV_SEQ_5 0x08
#define DRV2667_WV_SEQ_6 0x09
#define DRV2667_WV_SEQ_7 0x0A
#define DRV2667_FIFO 0x0B
#define DRV2667_PAGE 0xFF
#define DRV2667_MAX_REG DRV2667_PAGE
#define DRV2667_PAGE_0 0x00
#define DRV2667_PAGE_1 0x01
#define DRV2667_PAGE_2 0x02
#define DRV2667_PAGE_3 0x03
#define DRV2667_PAGE_4 0x04
#define DRV2667_PAGE_5 0x05
#define DRV2667_PAGE_6 0x06
#define DRV2667_PAGE_7 0x07
#define DRV2667_PAGE_8 0x08
/* RAM fields */
#define DRV2667_RAM_HDR_SZ 0x0
/* RAM Header addresses */
#define DRV2667_RAM_START_HI 0x01
#define DRV2667_RAM_START_LO 0x02
#define DRV2667_RAM_STOP_HI 0x03
#define DRV2667_RAM_STOP_LO 0x04
#define DRV2667_RAM_REPEAT_CT 0x05
/* RAM data addresses */
#define DRV2667_RAM_AMP 0x06
#define DRV2667_RAM_FREQ 0x07
#define DRV2667_RAM_DURATION 0x08
#define DRV2667_RAM_ENVELOPE 0x09
/* Control 1 Register */
#define DRV2667_25_VPP_GAIN 0x00
#define DRV2667_50_VPP_GAIN 0x01
#define DRV2667_75_VPP_GAIN 0x02
#define DRV2667_100_VPP_GAIN 0x03
#define DRV2667_DIGITAL_IN 0xfc
#define DRV2667_ANALOG_IN (1 << 2)
/* Control 2 Register */
#define DRV2667_GO (1 << 0)
#define DRV2667_STANDBY (1 << 6)
#define DRV2667_DEV_RST (1 << 7)
/* RAM Envelope settings */
#define DRV2667_NO_ENV 0x00
#define DRV2667_32_MS_ENV 0x01
#define DRV2667_64_MS_ENV 0x02
#define DRV2667_96_MS_ENV 0x03
#define DRV2667_128_MS_ENV 0x04
#define DRV2667_160_MS_ENV 0x05
#define DRV2667_192_MS_ENV 0x06
#define DRV2667_224_MS_ENV 0x07
#define DRV2667_256_MS_ENV 0x08
#define DRV2667_512_MS_ENV 0x09
#define DRV2667_768_MS_ENV 0x0a
#define DRV2667_1024_MS_ENV 0x0b
#define DRV2667_1280_MS_ENV 0x0c
#define DRV2667_1536_MS_ENV 0x0d
#define DRV2667_1792_MS_ENV 0x0e
#define DRV2667_2048_MS_ENV 0x0f
/**
* struct drv2667_data -
* @input_dev - Pointer to the input device
* @client - Pointer to the I2C client
* @regmap - Register map of the device
* @work - Work item used to off load the enable/disable of the vibration
* @regulator - Pointer to the regulator for the IC
* @magnitude - Magnitude of the vibration event
**/
struct drv2667_data {
struct input_dev *input_dev;
struct i2c_client *client;
struct regmap *regmap;
struct work_struct work;
struct regulator *regulator;
u32 page;
u32 magnitude;
u32 frequency;
};
static struct reg_default drv2667_reg_defs[] = {
{ DRV2667_STATUS, 0x02 },
{ DRV2667_CTRL_1, 0x28 },
{ DRV2667_CTRL_2, 0x40 },
{ DRV2667_WV_SEQ_0, 0x00 },
{ DRV2667_WV_SEQ_1, 0x00 },
{ DRV2667_WV_SEQ_2, 0x00 },
{ DRV2667_WV_SEQ_3, 0x00 },
{ DRV2667_WV_SEQ_4, 0x00 },
{ DRV2667_WV_SEQ_5, 0x00 },
{ DRV2667_WV_SEQ_6, 0x00 },
{ DRV2667_WV_SEQ_7, 0x00 },
{ DRV2667_FIFO, 0x00 },
{ DRV2667_PAGE, 0x00 },
};
static int drv2667_set_waveform_freq(struct drv2667_data *haptics)
{
unsigned int read_buf;
int freq;
int error;
/* Per the data sheet:
* Sinusoid Frequency (Hz) = 7.8125 x Frequency
*/
freq = (haptics->frequency * 1000) / 78125;
if (freq <= 0) {
dev_err(&haptics->client->dev,
"ERROR: Frequency calculated to %i\n", freq);
return -EINVAL;
}
error = regmap_read(haptics->regmap, DRV2667_PAGE, &read_buf);
if (error) {
dev_err(&haptics->client->dev,
"Failed to read the page number: %d\n", error);
return -EIO;
}
if (read_buf == DRV2667_PAGE_0 ||
haptics->page != read_buf) {
error = regmap_write(haptics->regmap,
DRV2667_PAGE, haptics->page);
if (error) {
dev_err(&haptics->client->dev,
"Failed to set the page: %d\n", error);
return -EIO;
}
}
error = regmap_write(haptics->regmap, DRV2667_RAM_FREQ, freq);
if (error)
dev_err(&haptics->client->dev,
"Failed to set the frequency: %d\n", error);
/* Reset back to original page */
if (read_buf == DRV2667_PAGE_0 ||
haptics->page != read_buf) {
error = regmap_write(haptics->regmap, DRV2667_PAGE, read_buf);
if (error) {
dev_err(&haptics->client->dev,
"Failed to set the page: %d\n", error);
return -EIO;
}
}
return error;
}
static void drv2667_worker(struct work_struct *work)
{
struct drv2667_data *haptics = container_of(work, struct drv2667_data, work);
int error;
if (haptics->magnitude) {
error = regmap_write(haptics->regmap,
DRV2667_PAGE, haptics->page);
if (error) {
dev_err(&haptics->client->dev,
"Failed to set the page: %d\n", error);
return;
}
error = regmap_write(haptics->regmap, DRV2667_RAM_AMP,
haptics->magnitude);
if (error) {
dev_err(&haptics->client->dev,
"Failed to set the amplitude: %d\n", error);
return;
}
error = regmap_write(haptics->regmap,
DRV2667_PAGE, DRV2667_PAGE_0);
if (error) {
dev_err(&haptics->client->dev,
"Failed to set the page: %d\n", error);
return;
}
error = regmap_write(haptics->regmap,
DRV2667_CTRL_2, DRV2667_GO);
if (error) {
dev_err(&haptics->client->dev,
"Failed to set the GO bit: %d\n", error);
}
} else {
error = regmap_update_bits(haptics->regmap, DRV2667_CTRL_2,
DRV2667_GO, 0);
if (error) {
dev_err(&haptics->client->dev,
"Failed to unset the GO bit: %d\n", error);
}
}
}
static int drv2667_haptics_play(struct input_dev *input, void *data,
struct ff_effect *effect)
{
struct drv2667_data *haptics = input_get_drvdata(input);
if (effect->u.rumble.strong_magnitude > 0)
haptics->magnitude = effect->u.rumble.strong_magnitude;
else if (effect->u.rumble.weak_magnitude > 0)
haptics->magnitude = effect->u.rumble.weak_magnitude;
else
haptics->magnitude = 0;
schedule_work(&haptics->work);
return 0;
}
static void drv2667_close(struct input_dev *input)
{
struct drv2667_data *haptics = input_get_drvdata(input);
int error;
cancel_work_sync(&haptics->work);
error = regmap_update_bits(haptics->regmap, DRV2667_CTRL_2,
DRV2667_STANDBY, 1);
if (error)
dev_err(&haptics->client->dev,
"Failed to enter standby mode: %d\n", error);
}
static const struct reg_default drv2667_init_regs[] = {
{ DRV2667_CTRL_2, 0 },
{ DRV2667_CTRL_1, DRV2667_25_VPP_GAIN },
{ DRV2667_WV_SEQ_0, 1 },
{ DRV2667_WV_SEQ_1, 0 }
};
static const struct reg_default drv2667_page1_init[] = {
{ DRV2667_RAM_HDR_SZ, 0x05 },
{ DRV2667_RAM_START_HI, 0x80 },
{ DRV2667_RAM_START_LO, 0x06 },
{ DRV2667_RAM_STOP_HI, 0x00 },
{ DRV2667_RAM_STOP_LO, 0x09 },
{ DRV2667_RAM_REPEAT_CT, 0 },
{ DRV2667_RAM_DURATION, 0x05 },
{ DRV2667_RAM_ENVELOPE, DRV2667_NO_ENV },
{ DRV2667_RAM_AMP, 0x60 },
};
static int drv2667_init(struct drv2667_data *haptics)
{
int error;
/* Set default haptic frequency to 195Hz on Page 1*/
haptics->frequency = 195;
haptics->page = DRV2667_PAGE_1;
error = regmap_register_patch(haptics->regmap,
drv2667_init_regs,
ARRAY_SIZE(drv2667_init_regs));
if (error) {
dev_err(&haptics->client->dev,
"Failed to write init registers: %d\n",
error);
return error;
}
error = regmap_write(haptics->regmap, DRV2667_PAGE, haptics->page);
if (error) {
dev_err(&hap

APei
- 粉丝: 96
最新资源
- 网络卫士安全隔离与信息交换系统技术培训.pptx
- 信息安全体系结构信息系统安全体系研究.pptx
- 扩频通信中直接扩频系统的同步技术.doc
- (源码)基于ESP8266芯片的环境监测系统.zip
- 基于思维导图的网络营销课程教学改革与实践.pdf
- 数字图像处理技术-第02章-MATLAB图像处理简介.ppt
- 2023年9月计算机等级考试二级C语言上机题库100套光碟拷贝版.doc
- sybyl软件与计算机辅助药物设计.pptx
- 最新网络营销工作总结.docx
- 中南大学工程项目管理第三章工程项目投资控制.pptx
- 热油系统安全运行措施.doc
- 计算机信息网络安全检查项目表.doc
- 小学教师计算机培训工作总结.docx
- 中国电信主要网络介绍.ppt
- 软件工程导论习题答桉.doc
- 楼宇自动化与系统集成解决方案--浙大中控.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


