123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815 |
- #include "pt_regs.h"
- #define PT_PROXIMITY_NAME "pt_proximity"
- #define PT_PROXIMITY_REQUEST_EXCLUSIVE_TIMEOUT 1000
- #define PT_PROXIMITY_ON 0
- #define PT_PROXIMITY_OFF 1
- static inline struct pt_proximity_data *get_prox_data(struct device *dev)
- {
- struct pt_core_data *cd = dev_get_drvdata(dev);
- return &cd->pd;
- }
- static void pt_report_proximity(struct pt_proximity_data *pd,
- bool on)
- {
- int val = on ? PT_PROXIMITY_ON : PT_PROXIMITY_OFF;
- input_report_abs(pd->input, ABS_DISTANCE, val);
- input_sync(pd->input);
- }
- static void pt_get_touch_axis(struct pt_proximity_data *pd,
- int *axis, int size, int max, u8 *xy_data, int bofs)
- {
- int nbyte;
- int next;
- for (nbyte = 0, *axis = 0, next = 0; nbyte < size; nbyte++) {
- pt_debug(pd->dev, DL_INFO,
- "%s: *axis=%02X(%d) size=%d max=%08X xy_data=%p xy_data[%d]=%02X(%d) bofs=%d\n",
- __func__, *axis, *axis, size, max, xy_data, next,
- xy_data[next], xy_data[next], bofs);
- *axis = *axis + ((xy_data[next] >> bofs) << (nbyte * 8));
- next++;
- }
- *axis &= max - 1;
- pt_debug(pd->dev, DL_INFO,
- "%s: *axis=%02X(%d) size=%d max=%08X xy_data=%p xy_data[%d]=%02X(%d)\n",
- __func__, *axis, *axis, size, max, xy_data, next,
- xy_data[next], xy_data[next]);
- }
- static void pt_get_touch_hdr(struct pt_proximity_data *pd,
- struct pt_touch *touch, u8 *xy_mode)
- {
- struct device *dev = pd->dev;
- struct pt_sysinfo *si = pd->si;
- enum pt_tch_hdr hdr;
- for (hdr = PT_TCH_TIME; hdr < PT_TCH_NUM_HDR; hdr++) {
- if (!si->tch_hdr[hdr].report)
- continue;
- pt_get_touch_axis(pd, &touch->hdr[hdr],
- si->tch_hdr[hdr].size,
- si->tch_hdr[hdr].max,
- xy_mode + si->tch_hdr[hdr].ofs,
- si->tch_hdr[hdr].bofs);
- pt_debug(dev, DL_INFO, "%s: get %s=%04X(%d)\n",
- __func__, pt_tch_hdr_string[hdr],
- touch->hdr[hdr], touch->hdr[hdr]);
- }
- }
- static void pt_get_touch(struct pt_proximity_data *pd,
- struct pt_touch *touch, u8 *xy_data)
- {
- struct device *dev = pd->dev;
- struct pt_sysinfo *si = pd->si;
- enum pt_tch_abs abs;
- for (abs = PT_TCH_X; abs < PT_TCH_NUM_ABS; abs++) {
- if (!si->tch_abs[abs].report)
- continue;
- pt_get_touch_axis(pd, &touch->abs[abs],
- si->tch_abs[abs].size,
- si->tch_abs[abs].max,
- xy_data + si->tch_abs[abs].ofs,
- si->tch_abs[abs].bofs);
- pt_debug(dev, DL_INFO, "%s: get %s=%04X(%d)\n",
- __func__, pt_tch_abs_string[abs],
- touch->abs[abs], touch->abs[abs]);
- }
- pt_debug(dev, DL_INFO, "%s: x=%04X(%d) y=%04X(%d)\n",
- __func__, touch->abs[PT_TCH_X], touch->abs[PT_TCH_X],
- touch->abs[PT_TCH_Y], touch->abs[PT_TCH_Y]);
- }
- static void pt_get_proximity_touch(struct pt_proximity_data *pd,
- struct pt_touch *tch, int num_cur_tch)
- {
- struct pt_sysinfo *si = pd->si;
- int i;
- for (i = 0; i < num_cur_tch; i++) {
- pt_get_touch(pd, tch, si->xy_data +
- (i * si->desc.tch_record_size));
-
- if (tch->abs[PT_TCH_O] == PT_OBJ_PROXIMITY) {
- if (tch->abs[PT_TCH_E] == PT_EV_TOUCHDOWN)
- pt_report_proximity(pd, true);
- else if (tch->abs[PT_TCH_E] == PT_EV_LIFTOFF)
- pt_report_proximity(pd, false);
- break;
- }
- }
- }
- static int pt_xy_worker(struct pt_proximity_data *pd)
- {
- struct device *dev = pd->dev;
- struct pt_sysinfo *si = pd->si;
- struct pt_touch tch;
- u8 num_cur_tch;
- pt_get_touch_hdr(pd, &tch, si->xy_mode + 3);
- num_cur_tch = tch.hdr[PT_TCH_NUM];
- if (num_cur_tch > si->sensing_conf_data.max_tch) {
- pt_debug(dev, DL_ERROR, "%s: Num touch err detected (n=%d)\n",
- __func__, num_cur_tch);
- num_cur_tch = si->sensing_conf_data.max_tch;
- }
- if (tch.hdr[PT_TCH_LO])
- pt_debug(dev, DL_WARN, "%s: Large area detected\n",
- __func__);
-
- pt_debug(dev, DL_INFO, "%s: extract data num_cur_rec=%d\n",
- __func__, num_cur_tch);
- if (num_cur_tch)
- pt_get_proximity_touch(pd, &tch, num_cur_tch);
- else
- pt_report_proximity(pd, false);
- return 0;
- }
- static int pt_proximity_attention(struct device *dev)
- {
- struct pt_proximity_data *pd = get_prox_data(dev);
- int rc = 0;
- if (pd->si->xy_mode[2] != pd->si->desc.tch_report_id)
- return 0;
- mutex_lock(&pd->prox_lock);
- rc = pt_xy_worker(pd);
- mutex_unlock(&pd->prox_lock);
- if (rc < 0)
- pt_debug(dev, DL_ERROR, "%s: xy_worker error r=%d\n",
- __func__, rc);
- return rc;
- }
- static int pt_startup_attention(struct device *dev)
- {
- struct pt_proximity_data *pd = get_prox_data(dev);
- mutex_lock(&pd->prox_lock);
- pt_report_proximity(pd, false);
- mutex_unlock(&pd->prox_lock);
- return 0;
- }
- static int _pt_set_proximity_via_touchmode_enabled(
- struct pt_proximity_data *pd, bool enable)
- {
- struct device *dev = pd->dev;
- u32 touchmode_enabled;
- int rc;
- rc = _pt_request_pip_get_param(dev, 0,
- PT_RAM_ID_TOUCHMODE_ENABLED, &touchmode_enabled);
- if (rc)
- return rc;
- if (enable)
- touchmode_enabled |= 0x80;
- else
- touchmode_enabled &= 0x7F;
- rc = _pt_request_pip_set_param(dev, 0,
- PT_RAM_ID_TOUCHMODE_ENABLED, touchmode_enabled,
- PT_RAM_ID_TOUCHMODE_ENABLED_SIZE);
- return rc;
- }
- static int _pt_set_proximity_via_proximity_enable(
- struct pt_proximity_data *pd, bool enable)
- {
- struct device *dev = pd->dev;
- u32 proximity_enable;
- int rc;
- rc = _pt_request_pip_get_param(dev, 0,
- PT_RAM_ID_PROXIMITY_ENABLE, &proximity_enable);
- if (rc)
- return rc;
- if (enable)
- proximity_enable |= 0x01;
- else
- proximity_enable &= 0xFE;
- rc = _pt_request_pip_set_param(dev, 0,
- PT_RAM_ID_PROXIMITY_ENABLE, proximity_enable,
- PT_RAM_ID_PROXIMITY_ENABLE_SIZE);
- return rc;
- }
- static int _pt_set_proximity(struct pt_proximity_data *pd,
- bool enable)
- {
- if (!IS_PIP_VER_GE(pd->si, 1, 4))
- return _pt_set_proximity_via_touchmode_enabled(pd,
- enable);
- return _pt_set_proximity_via_proximity_enable(pd, enable);
- }
- static int _pt_proximity_enable(struct pt_proximity_data *pd)
- {
- struct device *dev = pd->dev;
- int rc = 0;
- pm_runtime_get_sync(dev);
- rc = pt_request_exclusive(dev,
- PT_PROXIMITY_REQUEST_EXCLUSIVE_TIMEOUT);
- if (rc < 0) {
- pt_debug(dev, DL_ERROR, "%s: Error on request exclusive r=%d\n",
- __func__, rc);
- goto exit;
- }
- rc = _pt_set_proximity(pd, true);
- if (rc < 0) {
- pt_debug(dev, DL_ERROR, "%s: Error on request enable proximity scantype r=%d\n",
- __func__, rc);
- goto exit_release;
- }
- pt_debug(dev, DL_INFO, "%s: setup subscriptions\n", __func__);
-
- _pt_subscribe_attention(dev, PT_ATTEN_IRQ, PT_PROXIMITY_NAME,
- pt_proximity_attention, PT_MODE_OPERATIONAL);
-
- _pt_subscribe_attention(dev, PT_ATTEN_STARTUP,
- PT_PROXIMITY_NAME, pt_startup_attention, 0);
- exit_release:
- pt_release_exclusive(dev);
- exit:
- return rc;
- }
- static int _pt_proximity_disable(struct pt_proximity_data *pd,
- bool force)
- {
- struct device *dev = pd->dev;
- int rc = 0;
- rc = pt_request_exclusive(dev,
- PT_PROXIMITY_REQUEST_EXCLUSIVE_TIMEOUT);
- if (rc < 0) {
- pt_debug(dev, DL_ERROR, "%s: Error on request exclusive r=%d\n",
- __func__, rc);
- goto exit;
- }
- rc = _pt_set_proximity(pd, false);
- if (rc < 0) {
- pt_debug(dev, DL_ERROR, "%s: Error on request disable proximity scan r=%d\n",
- __func__, rc);
- goto exit_release;
- }
- exit_release:
- pt_release_exclusive(dev);
- exit:
- if (!rc || force) {
- _pt_unsubscribe_attention(dev, PT_ATTEN_IRQ,
- PT_PROXIMITY_NAME, pt_proximity_attention,
- PT_MODE_OPERATIONAL);
- _pt_unsubscribe_attention(dev, PT_ATTEN_STARTUP,
- PT_PROXIMITY_NAME, pt_startup_attention, 0);
- }
- pm_runtime_put(dev);
- return rc;
- }
- static ssize_t pt_proximity_enable_show(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- struct pt_proximity_data *pd = get_prox_data(dev);
- int val = 0;
- mutex_lock(&pd->sysfs_lock);
- val = pd->enable_count;
- mutex_unlock(&pd->sysfs_lock);
- return scnprintf(buf, PT_MAX_PRBUF_SIZE, "%d\n", val);
- }
- static ssize_t pt_proximity_enable_store(struct device *dev,
- struct device_attribute *attr, const char *buf, size_t size)
- {
- struct pt_proximity_data *pd = get_prox_data(dev);
- unsigned long value;
- int rc;
- rc = kstrtoul(buf, 10, &value);
- if (rc < 0 || (value != 0 && value != 1)) {
- pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
- return -EINVAL;
- }
- mutex_lock(&pd->sysfs_lock);
- if (value) {
- if (pd->enable_count++) {
- pt_debug(dev, DL_WARN, "%s: '%s' already enabled\n",
- __func__, pd->input->name);
- } else {
- rc = _pt_proximity_enable(pd);
- if (rc)
- pd->enable_count--;
- }
- } else {
- if (--pd->enable_count) {
- if (pd->enable_count < 0) {
- pt_debug(dev, DL_ERROR, "%s: '%s' unbalanced disable\n",
- __func__, pd->input->name);
- pd->enable_count = 0;
- }
- } else {
- rc = _pt_proximity_disable(pd, false);
- if (rc)
- pd->enable_count++;
- }
- }
- mutex_unlock(&pd->sysfs_lock);
- if (rc)
- return rc;
- return size;
- }
- static DEVICE_ATTR(prox_enable, 0600,
- pt_proximity_enable_show,
- pt_proximity_enable_store);
- static int pt_setup_input_device_and_sysfs(struct device *dev)
- {
- struct pt_proximity_data *pd = get_prox_data(dev);
- int signal = PT_IGNORE_VALUE;
- int i;
- int rc;
- rc = device_create_file(dev, &dev_attr_prox_enable);
- if (rc) {
- pt_debug(dev, DL_ERROR, "%s: Error, could not create enable\n",
- __func__);
- goto exit;
- }
- pt_debug(dev, DL_INFO, "%s: Initialize event signals\n",
- __func__);
- __set_bit(EV_ABS, pd->input->evbit);
-
- for (i = 0; i < NUM_SIGNALS(pd->pdata->frmwrk); i++) {
- signal = PARAM_SIGNAL(pd->pdata->frmwrk, i);
- if (signal != PT_IGNORE_VALUE) {
- input_set_abs_params(pd->input, signal,
- PARAM_MIN(pd->pdata->frmwrk, i),
- PARAM_MAX(pd->pdata->frmwrk, i),
- PARAM_FUZZ(pd->pdata->frmwrk, i),
- PARAM_FLAT(pd->pdata->frmwrk, i));
- }
- }
- rc = input_register_device(pd->input);
- if (rc) {
- pt_debug(dev, DL_ERROR, "%s: Error, failed register input device r=%d\n",
- __func__, rc);
- goto unregister_enable;
- }
- pd->input_device_registered = true;
- return rc;
- unregister_enable:
- device_remove_file(dev, &dev_attr_prox_enable);
- exit:
- return rc;
- }
- static int pt_setup_input_attention(struct device *dev)
- {
- struct pt_proximity_data *pd = get_prox_data(dev);
- int rc;
- pd->si = _pt_request_sysinfo(dev);
- if (!pd->si)
- return -EINVAL;
- rc = pt_setup_input_device_and_sysfs(dev);
- if (!rc)
- rc = _pt_set_proximity(pd, false);
- _pt_unsubscribe_attention(dev, PT_ATTEN_STARTUP,
- PT_PROXIMITY_NAME, pt_setup_input_attention, 0);
- return rc;
- }
- int pt_proximity_probe(struct device *dev)
- {
- struct pt_core_data *cd = dev_get_drvdata(dev);
- struct pt_proximity_data *pd = &cd->pd;
- struct pt_platform_data *pdata = dev_get_platdata(dev);
- struct pt_proximity_platform_data *prox_pdata;
- int rc = 0;
- if (!pdata || !pdata->prox_pdata) {
- pt_debug(dev, DL_ERROR,
- "%s: Missing platform data\n", __func__);
- rc = -ENODEV;
- goto error_no_pdata;
- }
- prox_pdata = pdata->prox_pdata;
- mutex_init(&pd->prox_lock);
- mutex_init(&pd->sysfs_lock);
- pd->dev = dev;
- pd->pdata = prox_pdata;
-
- pt_debug(dev, DL_INFO,
- "%s: Create the input device and register it\n", __func__);
- pd->input = input_allocate_device();
- if (!pd->input) {
- pt_debug(dev, DL_ERROR, "%s: Error, failed to allocate input device\n",
- __func__);
- rc = -ENODEV;
- goto error_alloc_failed;
- } else
- pd->input_device_allocated = true;
- if (pd->pdata->inp_dev_name)
- pd->input->name = pd->pdata->inp_dev_name;
- else
- pd->input->name = PT_PROXIMITY_NAME;
- scnprintf(pd->phys, sizeof(pd->phys), "%s/input%d", dev_name(dev),
- cd->phys_num++);
- pd->input->phys = pd->phys;
- pd->input->dev.parent = pd->dev;
- input_set_drvdata(pd->input, pd);
-
- pd->si = _pt_request_sysinfo(dev);
- if (pd->si) {
- rc = pt_setup_input_device_and_sysfs(dev);
- if (rc)
- goto error_init_input;
- rc = _pt_set_proximity(pd, false);
- } else {
- pt_debug(dev, DL_ERROR, "%s: Fail get sysinfo pointer from core p=%p\n",
- __func__, pd->si);
- _pt_subscribe_attention(dev, PT_ATTEN_STARTUP,
- PT_PROXIMITY_NAME, pt_setup_input_attention,
- 0);
- }
- return 0;
- error_init_input:
- input_free_device(pd->input);
- pd->input_device_allocated = false;
- error_alloc_failed:
- error_no_pdata:
- pt_debug(dev, DL_ERROR, "%s failed.\n", __func__);
- return rc;
- }
- int pt_proximity_release(struct device *dev)
- {
- struct pt_proximity_data *pd;
-
- if (dev)
- pd = get_prox_data(dev);
- else
- return 0;
-
- if (pd && pd->input_device_registered) {
-
- pd->input_device_registered = false;
- mutex_lock(&pd->sysfs_lock);
- if (pd->enable_count)
- _pt_proximity_disable(pd, true);
- mutex_unlock(&pd->sysfs_lock);
- device_remove_file(dev, &dev_attr_prox_enable);
- input_unregister_device(pd->input);
-
- pd->input_device_allocated = false;
- } else if (pd && pd->input_device_allocated) {
- pd->input_device_allocated = false;
- input_free_device(pd->input);
- _pt_unsubscribe_attention(dev, PT_ATTEN_STARTUP,
- PT_PROXIMITY_NAME, pt_setup_input_attention,
- 0);
- }
- return 0;
- }
|